What is a Cookie?

  
A cookie is often used to identify a user. A cookie is a file that the server Store on the user's computer. Each time the same computer requests a page with a browser, when the user click remember me ( Like as :  Remember me in Login Page ) and comes again this page that information already filled.

How To Set The Cookie

    setCookie('user','name',time()); 


<?php

$value = 'CodeHubDeep';

setcookie("Cookie_Name", $value);

setcookie("Cookie_Name", $value, time()+7200);  /* expire in 2 hour */

setcookie("Cookie_Name", $value, time()+7200, "/CodeHubDeep/", "https://codehubdeep.blogspot.com/", 1);

?>

 

This logic will be useful in case of Remember me.


Working With Cookie in PHP


How to expire Cookie : 


 since timestamp is independent unit and time cannot be < 0   hence timestamp cannot be destroy . 


<?php

setcookie("TestCookie", "", time() - 7200);

setcookie("TestCookie", "", time() - 7200, "/CodeHubDeep/", "https://codehubdeep.blogspot.com/", 1);

?>


Working With Cookie in PHP


 How to replace the existing Cookie : 

there is no way to update a cookie. Just set (again) this cookie using setcookie.


     setCookie('user','new_value',time()-300) // 12:35


<?php

$cookie_name = 'CodeHubDeep';

$cookie_value = 'TheBestEducationalWebsite';

setcookie($cookie_name, $cookie_value, time() -300, '/');

?>

 

Working With Cookie in PHP


How To Print an Individual Cookie : 


<?php echo $_COOKIE["Cookie_Name"]; ?>


How To Debug/Test is to View All Cookies : 


<?php  print_r($_COOKIE); ?>