Wednesday, January 7, 2015

Working with PHP Cookies

Cookies are text files stored in client computer for tracking and verifying purposes. They are also used for identification purpose by web server. Cookies are stored by browsers in local machine for future use and references.They are usually set in HTTP header and  they always have an expiry date, which means the cookie gets expired after some time limit.


PHP Script (create.php)

 <?php  
 $cookie_name = "login";  
 $cookie_value = "admin";  
 setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day  
 ?>  
 <!DOCTYPE html>  
 <html lang="en">  
 <head>  
 <meta charset="UTF-8"/>  
 <title>nirajanghimirey's workshop</title>  
 </head>  
 <body>  
 <?php  
 if(!isset($_COOKIE[$cookie_name])) {  
   echo "Cookie named '" . $cookie_name . "' is not set!";  
 } else {  
   echo "Cookie '" . $cookie_name . "' is set!!!!!!<br>";  
   echo "Value is: " . $_COOKIE[$cookie_name];  
 }  
 ?>  
 </body>  
 </html> 

No comments:

Post a Comment