Magento: Set, Get, and Delete cookie

Set a cookie with cookie name equals to sweetcookie, cookie value equals to chocolate chips, and expries 30 days from now. The set function can take 7 parameters, the first two are required, the others are optional.
* @param string $name The cookie name
* @param string $value The cookie value
* @param int $period Lifetime period
* @param string $path
* @param string $domain
* @param int|bool $secure
* @param bool $httponly

//60s * 60m * 24h * 30d
Mage::getModel('core/cookie')->set('sweetcookie','chocolate chips', 60*60*24*30);

Get the cookie value, and print it out. It will print chocolate chips below because we set it in the above code. If the cookie name doesn’t exist, it will return false, otherwise it returns the cookie value.

$cookieValue = Mage::getModel('core/cookie')->get('sweetcookie');

Get all cookies, they will be return in an array.

$cookies = Mage::getModel('core/cookie')->get();

Delete a cookie. The code below will delete the cookie sweetcookie. Another way to delete the cookie is to set the time of the cookie in the past, or 1 second from now. The code will be exactly the same as setting the cookie, except the third parameter for the time period.

Mage::getModel('core/cookie')->delete('sweetcookie');

The source code of the cookie class app/code/core/Mage/Core/Model/Cookie.php

Search within Codexpedia

Custom Search

Search the entire web

Custom Search