Certified PHP Developer Learning Resources Cookie Setting or Removal

Cookie Setting or Removal
 


/* Add Cookies */
SETCOOKIE ("TestCookie", $value);
SETCOOKIE ("TestCookie", $value,TIME() 3600); /* expire in 1 hour */
SETCOOKIE ("TestCookie", $value,TIME() 3600, "/~rasmus/", ".utoronto.ca", 1);
?>

We will use the setcookie() function to do this. The parameters used in this example are:

"name " This will be the name of the cookie. "value" The value of the cookie. This value is stored on the clients computer; do not store sensitive information. Assuming the name is 'cookiename', this value is retrieved through $_COOKIE['cookiename'] expire The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. In other words, you'll most likely set this with the time() function plus the number of seconds before you want it to expire. Or you might use mktime(). time()+60*60*24*30 will set the cookie to expire in 30 days. If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).

Note: You may notice the expire parameter takes on a Unix timestamp, as opposed to the date format Wdy, DD-Mon-YYYY HH:MM:SS GMT, this is because PHP does this conversion internally.

 

/* Remove Cookies */
// set the expiration date to one hour ago
SETCOOKIE ("TestCookie", "", TIME() - 3600);
SETCOOKIE ("TestCookie", "", TIME() - 3600, "/~rasmus/", ".utoronto.ca", 1);
?>

To remove a cookie happens simply by resetting the cookie. Instead of setting the cookie the value for time will be put to minus value for the previous plus value.

 For Support