Accessing PHP Cookies from different directories or domain path

February 25, 2010

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.

<?php 

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

<?php 

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

<?php
$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 domain.com/user/setcookie.php you cannot retrieve the cookie  on domain.com/member/getcookie.php. To get around this we need to set an accecibility parameter as well. See below.

<?php

// 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’);

1 Comment

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

Comments are closed.

Latest Posts

Rooturaj Pattanaik

Technology & Business Consultant with customers across 6 countries. A doting father and loving husband writing about various topics like Technology, Travel, Society, Digital marketing, Investment consulting etc.

MY DICTIONARY PROJECT

I have started a new project called the Indic Dictionary. This will cover popular household words in India and what they are called in various languages. Eventually, I will make this an easy to use app where where people can easily find something like “hing in English” or “Tea Tree Oil in Hindi” or “carrom seeds in Urdu”.
Previous Story

Oasis troops statistics | three kingdoms MMORPG game

Next Story

Happy Holi EveryOne

Latest from Programming

Go toTop

Don't Miss