In this tutorial you will learn what is a PHP cookie? And what’s the use of cookies in PHP. Cookie is one of the most important feature used while you are creating any website.
As you might be aware http is a stateless protocol, it cannot remember information between pages. A way to remember information from page to page is to use cookies. This is one of the main reason why we use cookies.
A cookie is text files stored at client side which is generally used to identify and store some of the less secure but frequently used information on client machine. Its stored at client side as text files. Once cookies are created each time a user requests page from server the server along with page also sends cookie.
You must have seen a e-commerce website remembers all the items you add into your cart throughout all the pages you visit, such tasks can be achieved using cookie.
For creating cookie variable setcookie() function is used.
Syntax
setcookie(Variablename,Value,Expirytime,Path,Domain);
Variable name parameter is mandatory, all other parameters are optional.
Variablename specifies the name of cookie variable which will be refereed with this specified named later using $_COOKIE[] global array.
Value specifies the value to be stored in cookie variable.
Expirytime specifies time after which your cookie variable will expire. The time will be given in UNIX timestamp format.
Path specifies the directory in which the cookie variable is accessible. By default its accessible from all directories.
Domain specifies the domain in which cookie is accessible. All cookies are only valid for the host and domain which created them.
Let’s take one example
<?php setcookie("name", "pratik", time()+3600); setcookie("salary", "50000", time()+3600,); ?>
As you can see in the example we have used setcookie() function for creating two cookie variables name & salary with values pratik & 50000.
We must learn one function.
It will return the time in UNIX timestamp format which is seconds since 00:00:00 GMT on 1st Jan 1970.
So in our example we have written time()+3600 which means we have set our expiry time to be 1 hour (60*60).
Now we are done creating cookie variables, next is how to access them? PHP provides two ways by which you can access created cookie variables.
For Ex.
<?php echo $_COOKIE["name"]. "<br />"; echo $HTTP_COOKIE_VARS["salary"] . "<br />"; ?>
To check if the cookie variable is set or not isset() function can be used as shown in example. isset() function checks if a variable is initialized or not and it returns Boolean value.
<?php if( isset($_COOKIE["name"])) { echo "Welcome " . $_COOKIE["name"] . "<br />"; } else { echo "Sorry... Not recognized" . "<br />"; } ?>
To modify the cookie variable which is already created all we need to do is call setcookie() function again with different value.
<?php setcookie("name", "deepen", time()+7200); ?>
As you can see we have modified the cookie variable name with new value deepen and new expiry time that is 2 hours.
To delete any cookie we have to use setcookie() function with any time from past.
<?php setcookie("name", "", time()-7200); ?>
Here we have given blank value to name variable with expiry time time()-7200 that is time in past. this way you can delete any cookie variable.
To conclude in this tutorial we have learnt what is cookie? And how to create , access and delete cookie in PHP.