Home Php PHP Cookies
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

PHP Cookies

Advertisement
In this section you will learn about the cookie management in PHP.

PHP Cookies

     

Cookies are little packets of information that are stored locally on a computer system when you visit a website that utilizes them. Cookies are  part of HTTP header, so cookies must be set before sending any value to the browser (i.e. before html tag).
Cookies can be used to store identifiable information about a user on their computer so that the website can pick up it later, even after your computer has been turned off, and you may continue from where you started.

Syntax of cookie is :

setcookie(name, value, expire, path, domain)

An example of this is with member systems and forums, when you are offered the "Remember me" option. With that, the website stores your username on your computer, and it can then find it when you log on to the website next, and enter your username automagically in the textbox.

Cookies are pretty easy to use, but you must remember that some people may not like the idea of having little pieces of information get stored on their computer, as the consequences other people might come to know about the secret data.

Example 1:

<?php 

setCookie("Name","roseindia");

if(isset($_COOKIE['Name']))

{

  echo $_COOKIE['Name'];

}

else

{

  echo "Cookie set, please refresh the page";

}

?>

Output:

roseindia

Example 2:

<?php

setCookie("Name","roseindia");

SETCOOKIE("ADDRESS","NEW DELHI");

SETCOOKIE("TIME",date("M d y"));

if(isset($_COOKIE['Name']))

{

  echo $_COOKIE['Name'];

}

else

{

  echo "Cookie set, please refresh the page";

}

print_r($_COOKIE);

?>

Output:

roseindiaArray
(
    [Name] => roseindia
    [ADDRESS] => NEW DELHI
    [TIME] => Nov 09 09
)

Example 3 (You can set the time of expiration):

If you want to set the time of expiration to 1 hr. then do as follows:

<?php

setCookie("Name","roseindia");

SETCOOKIE("ADDRESS","NEW DELHI");

SETCOOKIE("TIME",date("M d y"),time()+60);

if(isset($_COOKIE['Name']))

{

 echo $_COOKIE['Name'];

}

else

{

echo "Cookie set, please refresh the page";

}

print_r($_COOKIE);

?>

Output:

roseindiaArray
(
    [Name] => roseindia
    [ADDRESS] => NEW DELHI
    [TIME] => Nov 09 09
)

Certainly the output is not different from the previous one, but the values will not exist in the cookie more than one hour.

Example 4 (How to delete a cookie):

<?php

SETCOOKIE("user","",time()-3600);

if(isset($_COOKIE['Name']))

{

echo $_COOKIE['Name'];

}

else

{

echo "Cookie set, please refresh the page";

}

print_r($_COOKIE);

?>

Time should be set to previous time to clear the cookie.

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 9, 2009

Ask Questions?    Discuss: PHP Cookies  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Ashish Kumar
March 22, 2012
I want to make website

Please reply
mridul
April 9, 2012
php

chatting & sending email
Rohit chaturvedi
October 26, 2012
tanks

this is very useful sit for learner
mohammad atif
January 21, 2013
cookie time

i am expire the cookie value only for two second please give me solution
abhishek
September 19, 2013
php

i want to make a dynamic website .i have already make a static website
DMCA.com