Home » Magento31 December 2009

Magento: Create, Read, Delete Cookie

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.

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

Example / Usuage:-

Hope it helps. Thanks.


Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS 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?



  • 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?


  • @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);

  • 123123

  • googd

  • Hiren S


  • 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.

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