PHP Tutorials: Cookies

Do like cookies? Of course, everyone likes them. The crunch and the taste. But, do you know PHP also like cookies the same we do? Yes, I am not joking PHP also like cookies. The Cookie is a small text file which is used to store data on your computer. Where does it store the Cookie? Your guess is right, it’s your PC’s temporary memory. A Cookie is generally used to store the username, current time or any other information.

What is Cookie?

A Cookie is a small text file which is used to store a small amount of data in your computer for a particular website. The Cookie is stored in your computer that doesn’t mean they can be accessed by another website. If the Cookie was created by Facebook then it will be accessible to them only and if it was created by Gmail then it will be accessible only there. Although you can see, what Cookie data is being stored by Facebook or Gmail or any other website but you can’t access Cookie of Facebook on Gmail. Every website uses its own set of Cookie data. Some of them are very common in names like username and password. However, Cookie is a good technique to store data but it’s not 100% secure. It can be manipulated and can be deleted by you if you have a little knowledge of Cookie.

Wasn’t that a long story!! Let us move forward.

How to setup a Cookie?

PHP come with an inbuilt function to setup the Cookie data. The name of magical function is “setcookie()” it has 6 parameters.

  1. name: The name of the cookie for future reference
  2. value: Value to be stored in the cookie
  3. expires: The time it will expire. The time should be in UNIX timestamp format. After the expiration the Cookie will be inaccessible.
  4. path: It’s the path on which the cookie will be available. If you wanted to access the cookie in the entire website set it to “/”
  5. domain: Name of the domain for which the cookies will be accessible.
  6. secure: This field will indicate that, the value can only be accessed if HTTPS is available.

Syntax is:

setcookie(name, value, expires, path, domain, secure)

If you want the Cookie to expire just after the user close the browser then set it to 0 or simply ignore it.

Here is an example to store a Cookie value.

       //Set cookie for 30 days from the time visitor arrive on the page. 
       setcookie('author', 'Naresh Kumar', time()+30*24*60*60); 

The above Cookie we have stored an ‘author’ name ‘Naresh Kumar’ which will remain for the next 30 days ( 30 * 24 * 60 * 60).

How to access Cookie value?

It’s very easy to access a value which you was stored. PHP has a super-global variable $_COOKIE which holds all the values stored in Key-Value manner. $_COOKIE is an associative array, an individual value can be accessed with using standard array notation. Let us have a look at an example to access the value which we stored in the last example.

       //Access value. 
       echo $_COOKIE['author']; 

The above code will generate “Naresh Kumar” as an output.

If you wanted to avoid warning on the page it is always a best practice to check if the Cookie is available and has a value. Try this code to validate a Cookie.

       //isset will check if the coolie is set and has value. 
       if( isset($_COOKIE) && isset($_COOKIE['author']) && $_COOKIE['author'] != '' ) {
            echo $_COOKIE['author'];
       } else {
            echo 'Author has no value;'

How to remove/delete Cookie value?

A Cookie is a very strange thing. To set or delete you can use a single function setcookie() just with a small trick. to remove a cookie you have to set the expiry time to past. Let us have a look.

       //Set cookie for 30 days from the time visitor arrive on the page. 
       setcookie('author', 'Naresh Kumar', time()-3600); 

Isn’t it so easy? So we learned today all about Cookie. Hope you liked it. If you have any question ask in the comment section.

One thought on “PHP Tutorials: Cookies

Leave a Reply

Your email address will not be published. Required fields are marked *