Cookies are used by web sites to store information about the visitor at the local computer ( User end ) through the browser.
The difference between session and cookies is the cookies are stored at client computer and the sessions are stored at server side.
Sending cookies by setcookie
setcookie('Name','value','Expire duration','path','domain', 'secure','httponly')
Name Required, Name of the cookies to indentify
Value Optional , Value to be stored against the Name
Expire Optional , Duration to expire , set relative to current time() in seconds.
path Optional , Should be available throughout domain or particular directory or subdomain
domain Optional , Subdomain to which the cookie is available
secure Optional , if set to true then cookie will be set only when secure connection is available
httponly Optional , if set to true then cookie will be set accessible only through HTTP protocal
Example 1 : Setting the cookies
We will see how to add the cookies first. You can download the free code at the end of this page for your testing. You can use the demo here also. Here is the code to do that.
The above line will set the cookie with the variable value set to a string. This will expire after one hour as the time of expire is set to one huour ( +3600 seconds ) after the current time.
setcookie ("Plus2netCookie", $value,time()+3600);
/* expire in 1 hour */
We have used
to get the present time value ( ( time elapsed between current time and Unix Epoch (January 1 1970 00:00:00 GMT) in seconds ) and then added the expire time of the cookies in seconds.
Two things we have to know here.
setcookie ("Plus2netCookie", $name,time()+60*60*24*30);
// Expire in 30 days
1. Cookie set by the page now will be available at next page.
2. Before sending any header to the browser ( before sending any html code ) the cookie has to be set. Otherwise error message we will get.
To read the cookie we can check the value as a variable or by using the code below.
In PHP 5 and above it is
echo "Welcome " .$HTTP_COOKIE_VARS["Plus2netCookie"];
echo "Welcome " .$_COOKIE["Plus2netCookie"];
Same way to delete the cookies we will set the same cookie again with expire time one hour before by using time()-3600 . That will delete the cookie from the computer.
This will delete the cookie.
setcookie ("Plus2netCookie", "", time() - 3600);
Advantage of Cookies
Better user experience : Site can store user preference or choice of options through cookies over a period so the visitor need not have to start from scratch.
NOTE : Sensitive information should not be stored by using Cookies
The user have full control over the cookies it want to store or not and for what level it wants to store. The browser setting for the cookies can be managed by going to
Unlike sessions cookies are managed by users through its web browser so we have to carefully select which data to be used through cookies.
Tools >> Internet Options >> Privacy then advanced button.
Storing PHP array data in cookies
We can create an array and store it as cookie. Read the array data to display and destroy the data.
Download Zip file of all the demo scripts including the data array cookies ( cookies.zip)