In this chapter, we would learn how to work with cookies in PHP. At the end, you will be able to set cookies, retrieve the cookies and delete them. The following is covered here:
1. Overview of PHP Cookies
Cookies are bits of information placed on your computers by websites you visit. It is stored in form of text files and used for tracking and other things. PHP support cookies based on the following steps:
- a server script(PHP) sends some data in form of cookies to your browser. It may be name, age, time of login, some id etc
- the browser stores the information in the disk of the client machine
- next time the browser makes a request to the server, it attaches those information. So when the server gets it, it could now identify the source
- information in cookie is accessed using $_COOKIE or $_HTTP_COOKIE_VAR[] variables
2. Setting Cooking Values
You can set cookie values using the setcookie() function in PHP. This function takes up to six parameters. Moreover, it must be called before any output of your web page including the <html> and <head> tags and whitespace. The following are the parameters to the setcookie() function:
name – specifies the name of the cookie
value – the value of the cookie. It is recommended not to store any sensitive information. If the name the cookie is ssn, the the value can be retrieved using $COOKIE[“ssn”].
expires – this set the time the cookie expires.
path – this is the path on the server where the cookie will be stored. If this path is set to slash, then the cookie will be available to the whole domain.
domain – this is the domain the cookie is available to. For example www.kindsonthegenius.com
secure – this is a boolean value that specifies if the cookie should be transmitted over a secure HTTPS connection
The code below stores the value of name and score as cookies
<?php //Setting cookie values setcookie("name", "Kindson", time()+6000, "/","",TRUE,FALSE); setcookie("score", "99.9", time()+3400, "/","",0,1); ?> <html> <body> <?php echo "Cookie values of name and score is set"; ?> </body> </html>
3. Retrieving Cookie Values
PHP provides a number of ways to access cookie values. You can use either the $_COOKIE or the $HTTP_COOKIE_VARS variable. The example below illustrates this
<html> <body> <?php //the name cookie echo "Cookie value of name ". $_COOKIE["name"]; echo $HTTP_COOKIE_VARS["name"]; //same as above // the score cookie echo "Score is ". $_COOKIE["score"]; ?> </body> </html>
The code would output the value of the name and score cookies.
The isset() function is used to check is a cookie is set or not. It returns true is the cookie is set, otherwise it returns false.
4. Deleting Cookies
According to the official documentation, you can delete a cookie using the setcookie() function and providing the name argument only. However, some experts say theres is a better way. And that is to force the cookie to expire by setting it also using an already expired date.
An example is given below:
<?php //Setting cookie values to expired date setcookie("name", "", time()-60, "/","",FALSE); setcookie("score", "", time()-60, "/","",FALSE); ?> <html> <body> <?php echo "Deleting cookies"; ?> </body> </html>