Working with Cookies in PHP

Manas Singh 21st Sep 2016

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.

What is Cookie?

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.

How to create a cookies in PHP

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.

time()

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).

Accessing Cookies in PHP

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.

  1.  By using $_COOKIE global array
  2. By using $HTTP_COOKIE_VARS variable

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 />";
    }
?>

Modifying a cookie variable

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.

Deleting a cookie variable

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.

Some facts to remember before using cookies

  • Cookies are stored at client side, so it is advised not to store secure data in cookie.  Cookies can be enabled or disabled by client on their wish.
  • Cookie variable names are case sensitive. For ex. Username and username is different.
  • It requires less memory, no resources from server are used when we access cookie.
  • Cookies are browser specific; cookies created for one browser can’t be accessed in other browser.
  • Client can disable or delete any cookie variable.
  • Cookies do not allow storing binary data. It cannot store images and documents.

 To conclude in this tutorial we have learnt what is cookie? And how to create , access and delete cookie in PHP.

Authored By Manas Singh

He is a continuous blogger and has blogged on different topic. He loves to surf Internet and always trying to get new Idea about new Technology and Innovations and sharing these great information to all the technology lovers.

Also on DiscussDesk