Filed Under (Articles, PHP Code Samples) by Virender on May-29-2007

Cookies and Sessions both fulfill the purpose of storing data across the pages of a website. Both have their own advantages.

Cookies are stored on the client side and can be set to a long lifespan, which means that data stored in a cookie can be stored for months if not years. Cookies work fine with a web application installed on a cluster of web server, whereas sessions are stored on the server, means if one web server is handling the request, the other web server on the cluster will not have access to the information stored.

As Sessions are stored on web server, this gives extra security and freedom. We do not transmit session data with each page, just an ID and the data is loaded from the local file stored on the server. Clients do not know about the information we store about them, which is not the case with Cookies.

Many browsers have a limit for the Cookies as to stop the wastage of bandwidth that is caused by meaningless Cookies, but that is not the case with Sessions. They are stored on the server, hence they can be of any size. One can use them as per requirement. If he wants to store information until visitor comes back next day then Cookies are the way to go. If some part of cookie data is crucial then we can store it in database and just store an ID in the Cookie to reference the data. If not, then use Sessions, as they are little easier to use and do not require their entire data to be sent with each page. Sessions get cleaned as soon as visitor closes the web browser.

Let’s see how to use Cookies in a web page

The following script will store a Cookie labeled test on the client side with a value as “IsOn”. Please make sure that you set cookie values before sending any output to browser.

Code (php)

<?php
        setcookie("test", "IsOn", time() + (60 * 60));// variable name = test, value = "IsOn" and cookie life is one minute from current time. If you don't set cookie life, it will be expired after browser is closed.
?>
 

Now that our Cookie is set up, we can use it on every page of the site.

Code (php)

<?php
   echo $_COOKIE['test']; //displays "IsOn"
?>
 

If the above code shows nothing in output, then consider it as cookies being disabled or cookies improperly set. As you can see, we access information stored in cookies by using $_COOKIE[] array.

To destroy Cookies forcefully, we can use the following script:

Code (php)

<?php
        setcookie("test", "IsOn", time() - (60 * 60)); // setting a cookie life to a time that is past will actually expire the cookie instantly
?>
 

Now, let’s see how to use Sessions in a web page:

Code (php)

<?php
    session_start(); // this needs to be declared on the top of every page, specially before sending any output to the browser.
    $_SESSION['Check'] = 'test';
    echo $_SESSION['Check'];// displays "test" on any page of that web site
?>
 

We can use sessions on same page. Since sessions are server-side and the users have access to only the Session Identifier.

To destroy the Sessions for the current site, we can use the following script.

Code (php)

<?php
        session_destroy();
?>
 


Shamim on September 2nd, 2007 at 12:36 am #

No indication how to create encrypted cookie

subhendu on November 16th, 2007 at 3:37 pm #

how shall i prevent resendin same postdata to a mysql database

Traveler on August 22nd, 2008 at 6:13 pm #

Program that do this…

You get any webpage, text file etc. that has text links in it. So links are in text mode, not clickable.

You open the program, open empty text editor, copy all text from those mentioned before and paste all into blanc text editor, notepad etc. Then just click on OK button of the program, and it will collect all links converting them into clickable ones one under other in new html page.

What is the program? Where could it be found? I had it ones but lost it during HD brakedown.

Wrong address for this? nopr a?

manit kumar bhardwaj on March 31st, 2009 at 1:16 pm #

hi..
i want to full code of cookies

Post a comment
Name: 
Email: 
URL: 
Comments: