Site Navigation

Dec 29, 2008

Chewing on Cookies

On vacation this week, but have been doing a little work here and there. Currently I'm figuring out how to work with cookies using Dojo and PHP. So here are a few quick examples if you are ever interested.

Set a Cookie in PHP

setcookie('CookieName', 'cookie value', time() + 3600 * 24);

Just call the setcookie() function. Set the cookie name and and a value as shown. Notice how the time is set. Get the current time, then add the number of seconds you want the cookie to last. In this example, the cookie would be set to last 24 hours.

Get a Cookie with PHP

$cookieVar = $_COOKIE["CookieName"];

To get a cookie, just access the global $_COOKIE variable.

Delete a Cookie with PHP

setcookie ("CookieName", "", time() - 3600);

To delete a a cookie, set the cookie with an empty value and a time value in the pass. This should cause the browser to delete the cookie.

Get a Cookie with Dojo

// Initialize on Load

  var cookieVar = dojo.cookie("CookieName");


Call the dojo.cookie() function with the cookie name to get the value of the cookie.

Delete a Cookie with Dojo

function logOff(){
  dojo.cookie("CookieName", "", {expires: -1});

To delete a cookie the approach is similar to PHP, set the cookie to an empty value and time, a day in the past.

Post a Comment

Favorite Links Feed