|
Home >> FAQs/Tutorials >> PHP Script Tutorials and Tips >> Index
PHP Script Tips - Understanding and Managing Cookies
By: FYICenter.com
Part:
1
2
3
4
5
6
7
(Continued from previous part...)
How To Remove a Cookie?
Once a cookie is sent from the server to the browser, there is no direct way for the server
to ask the browser to remove the cookie. But you can use the setcookie() function to send
the same cookie to browser with a negative expiration time, which will cause the browser
to expire (remove) the cookie immediately. The next sample PHP page will let you
remove "CouponNumber" and CouponValue" persisted by the previous tutorial exercise:
<?php
setcookie("CouponNumber","",time()-1);
setcookie("CouponValue","",time()-1);
print("<pre>\n");
print("2 cookies were delivered with past times.\n");
$count = count($_COOKIE);
print("$count cookies received.\n");
foreach ($_COOKIE as $name => $value) {
print " $name = $value\n";
}
print("</pre>\n");
?>
Open your browser to visit this page: http://localhost/removing_cookies.php.
You will see:
2 cookies were delivered with past times.
2 cookies received.
CouponNumber = 07470433
CouponValue = 100.00
Click the refresh button, you will see:
2 cookies were delivered with past times.
0 cookies received.
As you can see, both cookies are removed.
What Are Domain and Path Attributes for Cookies?
Cookies can also be defined with two other attributes:
- Domain - A cookie attribute that defines the domain name of Web servers where this cookie is valid.
Web browsers holding this cookie should not sent it back to any Web server outside the specified domain.
The default domain is the domain from which the cookie originally came from.
- Path - A cookie attribute that defines the path name of Web server document path where this cookie is valid.
Web browsers holding this cookie should not sent it back to the server when requesting any documents that are
outside the specified path. The default path is the root path.
How To Specify Domain and Path for a Cookie?
If you want to specify domain and path for cookie, you can use the setcookie() function
with two extra parameters. The sample PHP script below shows you how to set the domain
and path attributes for temporary and persistent cookies:
<?php
setcookie("LoginName","FYICenter", NULL, "/", ".fyicenter.com");
setcookie("PreferredColor","Blue", NULL, "/", ".fyicenter.com");
setcookie("CouponNumber","07470433",time()+60*60*24*7,
"/store", ".fyicenter.com");
setcookie("CouponValue","100.00",time()+60*60*24*7,
"/store", ".fyicenter.com");
print("2 temporary cookies were delivered.\n");
print("2 consistent cookies were delivered.\n");
?>
What Is the Common Mistake When Setting Path and Domain on Temporary Cookies?
A common mistake made by many PHP developers is using an empty string for the expiration time
parameter when setting path and domain for temporary cookies. The PHP script below shows an example
of this mistake:
<?php
# Incorrect use of setcookie()
setcookie("LoginName","FYICenter", "", "/", ".fyicenter.com");
# Correct use of setcookie()
setcookie("PreferredColor","Blue", NULL, "/", ".fyicenter.com");
?>
If you run this script, you will get an error:
PHP Warning: setcookie() expects parameter 3 to be long,
string given in \php_working_with_cookies.php on line 3
How Cookies Are Transported from Servers to Browsers?
Cookies are transported from a Web server to a Web browser in the header area
of the HTTP response message. Each cookie will be included in a separate "Set-Cookie:"
header line in the following format:
Set-Cookie: name=value; expires=time; path=pathVal; domain=domainVal
(Continued on next part...)
Part:
1
2
3
4
5
6
7
|