Magento 1.9 – Custom Cookie Not Being Set

cookiemagento-1.9

Hi I have added the following code to the bottom of my index.php for a seperate store view in a subfolder of my magento installation.


$name = 'test';
$value = 'testval';
// set cookie
Mage::getModel('core/cookie')->set($name, $value);
//Show Cookie
echo Mage::getModel('core/cookie')->get($name);

It isnt echoing anything and I have no cookie in chrome, does anyone know why this may be?

Thanks!

Best Answer

The set method in the cookie model uses the setcookie php function.
As you can see in the link, the cookie you set is available in the next request.

Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays. Note, superglobals such as $_COOKIE became available in PHP 4.1.0. Cookie values also exist in $_REQUEST.

You are trying to get the cookie in the same request.