|
Using Cookies
Part:
1
2
3
4
(Continued from previous part...)
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.
Conclusion
- setcookie() must be called before any output to the HTTP response. The main reason is that
PHP is not buffering the HTTP response. But you can alter this behavior by using ob_*() functions.
- A persistent cookie is stored in a cookie file on the browser's local machine.
- A persistent cookie can have a expiration time to be expressed in number of seconds since epoch.
- Web browser will only send back a cookie when both domain and path match the requested domain and path.
- To make a cookie available for all sub domains of a top level domain, set the domain property
to the top level domain name.
Part:
1
2
3
4
|