Set/Get/Remove Cookies

Description: Examples of setting, getting, and removing cookies.

/* Add Cookies */
<?PHP
SETCOOKIE ("TestCookie", $value);
SETCOOKIE ("TestCookie", $value,TIME() 3600); /* expire in 1 hour */
SETCOOKIE ("TestCookie", $value,TIME() 3600, "/~rasmus/", ".utoronto.ca", 1);
?>
 
/* Remove Cookies */
<?PHP
// set the expiration date to one hour ago
SETCOOKIE ("TestCookie", "", TIME() - 3600);
SETCOOKIE ("TestCookie", "", TIME() - 3600, "/~rasmus/", ".utoronto.ca", 1);
?>
 
/* Use Cookies */
<?PHP
ECHO $TestCookie;
ECHO $_COOKIE["TestCookie"];
?>
 
/* Set cookie array */
<?PHP
SETCOOKIE ("cookie[three]", "cookiethree");
SETCOOKIE ("cookie[two]", "cookietwo");
SETCOOKIE ("cookie[one]", "cookieone");
     IF (ISSET ($cookie)) {
          WHILE (LIST ($name, $value) = EACH ($cookie)) {
          ECHO "$name == $value<br />\n";
     }
}
?>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.