Share on Google+Share on Google+

PHP Cookies

Advertisement
PHP Cookies is used to sent the information by a web-server to a web-browser and whenever you accesses that server the browser will give you the same result without changing anything. Cookies in PHP is helpful to identify the user.

PHP Cookies

PHP Cookies is used to sent the information by a web-server to a web-browser and whenever you accesses that server the browser will give you the same result without changing anything. Cookies in PHP is helpful to identify the user.

If you are developing highly interactive website, it means you are going to deal with bunch of data flow between client browser and web server. It is very difficult to handle such large amount of data. This problem can be solved by using Cookies.

Cookies with PHP are helpful to create and retrieve cookies value.

setcookie() Function

    This function is used to set a cookie and also send the information along with the HTTP Headers.

Syntax for PHP Cookies

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

 Let's see the example below :

Example 1 :

<?php

SETCOOKIE("userName","Rose India");

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

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

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

{

echo "Welcome to ".$_COOKIE['userName']."!!"."<br>";

echo "Your address is ".$_COOKIE['ADDRESS']."!!"."<br/>";

echo "You are last visited on ".$_COOKIE['TIME']."!!"."<br/>";

}

else

{

echo "Cookies set,please refresh the page!!";

}

?>

The output of the above example is : Welcome to Rose India!!
                                                        Your address is NEW DELHI!!
                                                        You are last visited on Jan 18 10!!

 Cookies Array

   Example 1 :

<?php

SETCOOKIE("userName","Rose India");

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

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

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

{

print_r($_COOKIE);

}

else

{

echo "Cookies set,please refresh the page!!";

}

?>

The output of the above example is : Array ( [userName] => Rose India [ADDRESS] => NEW DELHI [TIME] => Jan/18/10 [cookie] => Array ( [username] => ROSEINDIA [address] => NEWDELHI [time] => Jan/18/10 ) )

 

 Example 1 :

<?php

setcookie("cookie[username]", "ROSEINDIA");

setcookie("cookie[address]", "NEWDELHI");

setcookie("cookie[time]", date("M/d/y"));

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

{

foreach ($_COOKIE['cookie'] as $name => $value)

{

echo "$name : $value <br />\n";

}

}

?>

The output of the above example is : username => ROSEINDIA
                                                        address => NEWDELHI
                                                        time => Jan/18/10

Advertisements

Advertisement

Posted on: January 18, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

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.