Home » Magento

Magento: Create, Read, Delete Cookie

31 December 2009 Share/Bookmark

Here is the code to create, read, and delete cookie in Magento.
Mage_Core_Model_Cookie class contains functions to set, get and delete cookie.

/**
 * set cookie
 * name and value are mandatory; other parameters are optional and can be set as null
 * $period = cookie expire date
 */
 Mage::getModel('core/cookie')->set($name, $value, $period, $path, $domain, $secure, $httponly);
/**
 * get cookie with a specific name
 * $name = name of the cookie
 */
 Mage::getModel('core/cookie')->get($name);
 /**
 * get all cookies as an array
 */
 Mage::getModel('core/cookie')->get();
 /**
 * delete/remove cookie
 * $name is mandatory; other parameters are optional and cen be set as null
 */
 Mage::getModel('core/cookie')->delete($name, $path, $domain, $secure, $httponly);

You can get cookie expire date, path, domain, secure, httponly from the following Magento code.

$cookieExpires = Mage::getModel('core/cookie')->getLifetime($cookieName);
$cookiePath = Mage::getModel('core/cookie')->getPath($cookieName);
$cookieDomain = Mage::getModel('core/cookie')->getDomain($cookieName);
$cookieSecure = Mage::getModel('core/cookie')->isSecure($cookieName);
$cookieHttponly = Mage::getModel('core/cookie')->getHttponly($cookieName);

Example / Usuage:-

$name = 'test';
$value = 'mukesh';

// set cookie
Mage::getModel('core/cookie')->set($name, $value);

// print cookie value
echo Mage::getModel('core/cookie')->get($name);

// print cookie lifetime or period
// by default the period is set as 3600 seconds
echo Mage::getModel('core/cookie')->getLifetime($name);

// you can set different period value as well
$name = 'test2';
$value = 'chapagain';
$period = 7200;
Mage::getModel('core/cookie')->set($name, $value, $period);

Hope it helps. Thanks.

From Mukesh Chapagain's Blog, post Magento: Create, Read, Delete Cookie

email

php magento mukesh chapagain

Get New Post by Email
RSS Feed Subscribe RSS Feed
  • Drew

    Hi, I also noticed this class, but it has a part in it that won’t set the cookies if headers are already set. Where then can someone actually set a cookie? Obviously not in template files because headers are already sent. How would I call this class from before headers are sent?

    Thanks–

    Drew

  • Gregg

    First off, great blog. very nicely laid out content, especially for Mage!

    My question for you was this:
    This is exactly what I am looking for, but I am unsure where to place it. If I put it in the template files (.phtml) it gives an error. any suggestions on where to place the custom cookies code I want to create?

    Thanks!

  • http://blog.chapagain.com.np Mukesh

    @Gregg – The code should work on phtml file as well. What error do you get?

  • Kat

    I also had a problem setting a cookie and also wanted to set it in .phtml file.
    At the end I got it to work, so I’m now setting a cookie in .phtml and retrieving it in Block file (for that .phtml file).
    What I’ve learned was:
    - when setting a cookie be consistent when using quotes, for example use double quotes OR use single quotes – never both at the same time (ie set(“cookie-name”,”cookie-value”,time()+36000,”/”)
    Never use it like this: (“cookie-name”, ‘cookie-value’)
    - you can leave domain setting empty – I wanted to set that as well, well it turned out that Magento set() function checks if domain parameter is empty, then it sets a domain.
    - you can set cookie anywhere (php or phtml files) but to retrieve a cookie I suggest putting it in some block or model file, cause if you’re getting a cookie value in .phtml it might break the html (the site view) – well, that happened to me.
    Perhaps just to mention the files I was using:
    */app/design/frontend/…/…/template/catalog/product/list.phtml */app/code/local/Mage/Catalog/Block/Product/List.php

    Bye the way, when trying to figure out the problem I also found useful a php site on creating cookies http://php.net/manual/en/function.setcookie.php

    Thanks Mukesh for this post!

  • Alecu

    Nice article, thanks.

    Set a cookie:

    $id = ‘my_cookie’
    $data = ‘my_data’;
    $time = time()+36000;
    Mage::getModel(‘core/cookie’)->set($id, $data, $time);

    Get a cookie:

    $id = ‘my_cookie’;
    $data_from_cookie = Mage::getModel(‘core/cookie’)->get($id);
    print_r($data_from_cookie);

  • http://twitter.com/jayelan Jay Shah

    123123

  • http://twitter.com/jayelan Jay Shah

    googd

  • Hiren S

    hihihihi

  • Jake

    Just for reference sake, use the above methods for $period as seconds, i.e.: 86400 (24Hrs) and NOT as time()+86400. So, your code is like

    Mage::getModel(‘core/cookie’)->set(‘cookie-name’, ‘cookie-value’, 86400);

    You may also determine $period as TRUE and mage will auto set to expire in 365 days (a year).

    If set to NULL, the cookie will get the lifetime from the store config

    if using a 0 (zero) it will set to expire at end of session

    If you use any other way/method, your cookie will always expire at end of session.

    Below a snippet from Core_Model_Cookie:

            if ($period === true) {
                $period = 3600 * 24 * 365;
            } elseif (is_null($period)) {
                $period = $this->getLifetime();
            }

            if ($period == 0) {
                $expire = 0;
            }
            else {
                $expire = time() + $period;
            }

  • Bijal

    Hi, I have used above code to add new cookie, in magento 1.6.1 version, when i try to print cookie Mage::getModel(‘core/cookie’)->get(); but it is not showing me value which was set.

  • http://blog.chapagain.com.np/ Mukesh

    Please refresh the page again and see. It should show the cookie value.