Share on Google+Share on Google+

PHP Cookie

Advertisement
In this tutorial we will study about cookie in PHP, cookies are very important part of Internet, it is used to store the data at client's browser to minimize the travel to the server. Examples in this tutorial will make it more clear.

PHP-Cookies

Cookie is a very important part in Internet. PHP supports HTTP cookies. Using cookie we can store data into a remote browser. We can set the cookies using either setcookie() or setrawcookie() function. Cookies belong to the HTTP header that's why  setcookie() must be called before any output is sent to the browser like header() function.

Any cookie which is sent  from client side will automatically be included into a $_COOKIE auto global array. If more than one value has to be stored then we need to declare the $_COOKIE as associative array.

Example:

Cookie-1.php

<?php

$var="This is a test of cookie";

setcookie("TestCookie",$var);

?>

<form action="cookie-2.php" method="post">

Name<input type="text" name="name"/>

</form>

Cookie-2.php

<?php

if(isset($_POST))echo $_POST['name'];

echo $_COOKIE['TestCookie'];

echo $var;

?>

 

Output:

Enter an input and hit the return key,  and the output will be as follows:

roseindia: This is a test of cookie 

Advertisements

Advertisement

Posted on: March 31, 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.