Hello friends, this is my 10th tutorial of PHP and Today I am sharing the concept of using Cookies in PHP. Cookies are useful for storing small chunk of data at the client browser that you can use later for providing faster services to your user as you don't have to fetch that data from the server.
Creating cookies an saving it on client's browser :
 <?php  setcookie('test',200,time()+(60*60)); ?>
As you can see, I have passed three arguments while creating the cookie, the first argument specifies the name of the cookie, second argument specifies the value for the cookie and the third argument specifies the default expiration time of the cookie.
Fetching cookies that are saved on client's browser :
 <?php  
$var1=0;
if(isset($_COOKIE['test'])) {
 $var1=$_COOKIE['test'];
}
echo $var1;
?>
Remember to check if the cookie exist or not before fetching it as cookies can automatically get deleted once the it's expiration time is over.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.