It is pretty easy to work with cookies. They are really tasty. I am talking about PHP cookies and yeah we programmers can taste them. I will be highlighting some ‘not so common’ points about cookies today. Ready?? Lets Go.

What are Cookies?

These are little bits of information stored on your computer by a website. The specialty of these bits is that they can only be opened by the site that stores it in first place. So Google’s Cookies are invisible to Yahoo’s and same the other way round. Cookies can be set for a particular time span and deleted by the site that created it. The user can also clear cookies whenever he wants to from his browser or manually from the personal folders of his system.

Now we know that Cookies are store houses for secure information that we webmasters use to dish out customized content to our visitors. They are widely used for storing user login information and shopping cart contents so that even if the user closes the browser and comes back the site allows access and restores his shopping cart. We could even use it to store your theme choice for sites or to tell you when you visited last time.

How to set a cookie Using PHP?

Its pretty easy. Use this  code.


// set a cookie named 'pingu' with a value of 'Penguin'
setcookie('pingu', 'Penguin');


Recalling and using the set Cookie

We use the super global variable $_COOKIE  to  call the stored value of cookie the same way we do with $_GET and $_POST.


// We  will  use ‘pingu’ cookie  in a sentence.
echo "Pingu is a ".$_COOKIE['pingu']."."; 


Output: Pingu is a Penguin

Setting  a  time span for your cookie.

For security purposes we need to set some cookie expiry time. Some sites even ask the user how long to store his cookie. Others have a default expiry period. After this time your cookies is dead and you need to log in again to use the site. (or any other similar functionality)

$time = 7*24*3600 + time() ; //no of secs to store the cookie

// set a cookie named 'pingu' with a value of 'Penguin' for  2  days
setcookie('pingu', 'Penguin', $time);


Accessing a Cookies from a different directory of domain.

Now when using the previous methods to set cookie will restrict the access to the directory where it was originally set. Eg. If you set the cookie on file you cannot retrieve the cookie  on To get around this we need to set an accecibility parameter as well. See below.


// set a cookie named 'pingu' with a value of 'Penguin' for 2 days  through out the domain.
setcookie('pingu', 'Penguin', $time, “/”);


Now you cookie can be called from any part of the domain.  But it’s a not much recommended. Similarly you can specify particular directories/folders to access the cookie. Like “/user/” or “/member/” instead of “/”.

Overall Syntax format – setcookie (‘[string]’,’[string]’,[integer],’string’);

More posts you might be interested in:

PHP Image Resize script - Resize Images on the fly
Text content outside Div IE | Duplicate Characters Bug IE6
Software : Apache Spark and Zeppelin Installer for Windows

One Response so far.

  1. Thomas Jeffrey says:

    I will be very happy if you can tell me why it is not recommended to use the accessibility parameter in cookies

Leave a Reply