PHP Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 3.00

Other Cookie Properties - Domain and Path

This section describes two other cookie properties, domain and path, which tells the browser to send back the cookie only to the specified domain and path.

A cookie also has two other properties:

1. "domain" - A property that defines the domain of Web servers to which this cookie should be made available. Web browsers will send a cookie back to a Web server when the Web server matches its defined domain. Web browsers will never send back a cookie to a domain other than its defined domain.

For example, if a cookie's domain is www.google.com, the browser will send back this cookie to the server only when the browser is visiting www.google.com. The browser will never send back this cookie to www.yahoo.com.

To make a cookie available for all sub domains of a top level domain, set the domain property to the top level domain name. For example, if a cookie's domain is set to ".google.com", this cookie will be available to all google sub domains, like groups.google.com and gmail.google.com.

2."path" - A property that defines a Web server path to which this cookie should be made available. Web browsers will send a cookie back to a Web server when the Web server matches its defined domain, and the requested page matches its defined path. Web browsers will never send back a cookie to requested path other than its defined path.

Note that the defined path also includes all its sub paths. For example, if a cookie's domain is "www.amazon.com", and path is "/order/", then a browser will send back this cookie for requests like "http://www.amazon.com/order/checkout.html", and "http://www.amazon.com/order/report/invoice.html". But a browser should not send back this cookie for requests like "http://www.amazon.com/catelog/book.html".

The setcookie() function offers two more parameters to allow you to set "domain" and "path" properties on a cookie as in the following syntax:

bool setcookie(string name, string value, int expire, string path,
   string domain)

where "path" specifies the cookie's path property, and "domain" specifies the cookie's domain property. If "path" is not given, the cookie will have "/" as the default path. If "domain" is not given, the cookie will have the current domain as the default domain.

Okay. Let's play the properties with the following script, CookieProperties.php:

<?php #CookieProperties.php
# Copyright (c) 2005 by Dr. Herong Yang, http://www.herongyang.com/
#
   print("<pre>\n");
   print("\nAdding a cookie with default properties:\n");

   $cookieName = "User";
   $cookieValue = "Herong Yang";
   $expiration = time()+60*60*24*30;
   setcookie($cookieName, $cookieValue, $expiration);
   print("   Name: $cookieName\n");
   print("   Value: $cookieValue\n");
   print("   Expiration: $expiration\n");

   print("\nAdding a cookie with non-default properties:\n");
   $cookieName = "Book";
   $cookieValue = "Herong's Tutorial Notes on PHP";
   $expiration = time()+60*60*24*30;
   $path = "/";
   $domain = "localhost";
   setcookie($cookieName, $cookieValue, $expiration, $path, $domain);
   print("   Name: $cookieName\n");
   print("   Value: $cookieValue\n");
   print("   Expiration: $expiration\n");
   print("   Path: $path\n");
   print("   Domain: $domain\n");

   print("\nCookies received by the server:\n");
   foreach ($_COOKIE as $k => $v) {
      print "   $k = $v\n";
   }

   print "</pre>\n";
?>

Ran this script in IE, I got:

Adding a cookie with default properties:
   Name: User
   Value: Herong Yang
   Expiration: 1134622043

Adding a cookie with non-default properties:
   Name: Book
   Value: Herong's Tutorial Notes on PHP
   Expiration: 1134622043
   Path: /
   Domain: localhost

Clicked the refresh button on IE, I got:

Adding a cookie with default properties:
   Name: User
   Value: Herong Yang
   Expiration: 1134622059

Adding a cookie with non-default properties:
   Name: Book
   Value: Herong's Tutorial Notes on PHP
   Expiration: 1134622059
   Path: /
   Domain: localhost

Cookies received by the server:
   User = Herong Yang

Apparently, my script did not set the properties correctly. The browser should have sent back my second cookie also. So either the "path=/" or "domain=localhost" did not match my local IIS environment. I could not figure it out why.

Last update: 2005.

Sections in This Chapter

What Is a Cookie?

Sending and Receiving Cookies

Sending and Receiving Cookies - Example

ob_start() - Output Buffering Function

Persistent Cookies Saved on Hard Disk

Other Cookie Properties - Domain and Path

Dr. Herong Yang, updated in 2009
Other Cookie Properties - Domain and Path