PHP Cookie
Posted on: March 31, 2010 at 12:00 AM
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 

Related Tags for PHP Cookie:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.