PHP and Cookies - Creating, Reading and Writing
Previous | Table of Contents | Next |
PHP and HTML Forms | Understanding PHP Sessions |
Cookie Expiration Setting
The optional expires= section specifies the date on which the associated cookie should expire. The PHP time() function can be used to obtain and manipulate dates for this purpose as we will examine later in this chapter.
Cookie path Setting
The path= setting allows a URL to be stored in the cookie. By default, cookies are accessible only to web pages in the same directory as the web page which originally created the cookie. For example, if the cookie was created when the user loaded http://www.techotopia.com/intro/index.html that cookie will be accessible to any other pages in the /intro directory, but not to pages in /navigation. By specifying path=/navigation this limitation is removed.
Cookie domain Setting
Similar to the path setting, cookies are only accessible to web pages residing on the server domain from which the cookie was originally created. For example, a cookie created by a web page residing on www.techotopia.com is not, by default, accessible to a web page hosted on www.linuxtopia.org. Access to the cookie from web pages on linuxtopia.org can be enabled with a domain=linxutopia.org setting.
Cookie Security Setting
This setting controls whether the cookie is transmitted using insecure HTTP or secure HTTPS.
Creating a Cookie in PHP
Cookies are created in PHP using the setcookie() function. setcookie() takes a number of arguments. The first argument is the name of the cookie (the name part of the name/value pair described earlier). The second is the value part of the name/value pair. The third argument is the optional expiration date of the cookie. The fourth argument specifies the active path for the cookie. The fifth argument is the domain setting and the sixth is the security setting (0 specifies HTTP and HTTPS and 1 specifies HTTPS only).
Based on the above information we can create a cookie using the following PHP:
<?php setcookie('userName', 'JohnW', time() + 4800); echo 'Cookie has been set<br>'; ?>
The above example creates a cookie on the computer system of anyone who loads the page (assuming they have cookies enabled in their browser) containing the name value pair userName=JohnW'. The cookie will expire 4800 seconds from the time it is created.
Reading a Cookie in PHP
Given that you've gone to the trouble of writing a cookie it stands to reason you'll probably want to read it back at some point. This is achieved by accessing the $_COOKIE array. The $_COOKIE array is an associative array whereby the name of the cookie provides the index into the array to extract the corresponding value of the name/value pair (for details of PHP arrays read the PHP Arrays chapter of this book.
For example we can obtain the value of our userName cookie as follows:
<?php echo 'Reading cookie<br>'; echo 'userName = ' . $_COOKIE['userName']; ?>
The above script should generate the following output:
Reading cookie
userName = JohnW
Deleting a Cookie
Cookies are deleted by calling the setcookie() function with the cookie name, a null for the value and an expiration date in the past. Once again the time() function can be used to calculate an expired date:
<?php setcookie ('userName', '', time() - 4800); ?>
Note that if you specified domain and/or path arguments when you created the cookie you must also specify them when you delete the cookie.