Set get and delete cookies in magento either by server side php or client side javascript ways.
Set get and delete cookies in magento either by server side php or client side javascript ways.

Cookie ,what does it mean ? What I know a cookie is used to identify user’s identity .So how ? because a cookies is a small files that server embeds on the user’s computer.Each time same computer request a page with a browser, it will send the cookie too.So here I am going to explain we can use cookies in magento through php as well as javascript side.In magento we have a js file called cookies.js file in dir js/mage/cookies.js.In that files set get and delete function is defined .So simply If we want to add cookies in javascript file means by client side then we have to call magento Mage.Cookies.set function when we require then our problem is solved.Also For PHP side we have load the cookie model and set our desire params then cookie will be set.So I will describe here both javascript and php way.Lets see one by one.

For same artcile in Magento2 you may follow Set, get and delete data from cookie in magento 2

Client side:

If you see the file then you will get below code which has done cookies setting for magento. So we just have access all the variable and function declared in this function.If you will have a little look then you can understand better.

In client side we do by using javascript.So before setting cookie we have to set path,domain and expaire time.That means If we like to set our cookie for entire domain then we can just use ‘/’ or if we want set for specific url(directory) then we have to use ‘/directoryName/’ etc.

if (!window.Mage) var Mage = {};

Mage.Cookies = {};
Mage.Cookies.expires  = null;
Mage.Cookies.path     = '/';
Mage.Cookies.domain   = null;
Mage.Cookies.secure   = false;
Mage.Cookies.set = function(name, value){
     var argv = arguments;
     var argc = arguments.length;
     var expires = (argc > 2) ? argv[2] : Mage.Cookies.expires;
     var path = (argc > 3) ? argv[3] : Mage.Cookies.path;
     var domain = (argc > 4) ? argv[4] : Mage.Cookies.domain;
     var secure = (argc > 5) ? argv[5] : Mage.Cookies.secure;
     document.cookie = name + "=" + escape (value) +
       ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
       ((path == null) ? "" : ("; path=" + path)) +
       ((domain == null) ? "" : ("; domain=" + domain)) +
       ((secure == true) ? "; secure" : "");
};

Mage.Cookies.get = function(name){
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    var j = 0;
    while(i < clen){
        j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return Mage.Cookies.getCookieVal(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if(i == 0)
            break;
    }
    return null;
};

Mage.Cookies.clear = function(name) {
  if(Mage.Cookies.get(name)){
    document.cookie = name + "=" +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
};

Mage.Cookies.getCookieVal = function(offset){
   var endstr = document.cookie.indexOf(";", offset);
   if(endstr == -1){
       endstr = document.cookie.length;
   }
   return unescape(document.cookie.substring(offset, endstr));
};

Declare Path:
Mage.Cookies.path = '/';
Setting Domain:

The cookies will available for the domain www.domainName.com

Mage.Cookies.domain = '.domainName.com';
Set Expire Time:
var toDay = new Date();//for getting todays date.
toDay.setTime(toDay.getTime());
var expaireTime = 1 * 1000 * 60 * 60 * 24;
var expaireDate = new Date(toDate.getTime()+(expaireTime));
Mage.Cookies.expires = expaireDate;

So till now we just have declared some variable.Now we have to use the function.Suppose I want set the name of user in cookies so lets see how it works.


Set Cookie Value:
Mage.Cookies.set(cookie_name, cookie_value);
Example:
Mage.Cookies.set('user_name', 'jyotiranjan');

here cookie name is ‘user_name’ and its value is ‘jyotiranjan’

Get Cookie value:
Mage.Cookies.get(cookie_name);
Example:
var userName = Mage.Cookies.get('user_name');
alert('cookies user name is : '+userName);
Delete Cookie Value:
Mage.Cookies.clear(name);
Example:
Mage.Cookies.clear('user_name');

Server Side:

In server side magento do by using php.For setting,getting and deleting cookies by using php in magento we have to call cookie model the we will set out desire parameters.So I am using all the example that I have used in client side section.

So for doing In magento set get delete cookie has different functions lets starts one by one by using different examples.

My variable that I have used for references.

$name = ‘user_name’;//cookie name
$value = ‘jyotiranjan’;//cookie value
$period = 3600;//lifetime of cookie in sec
$path = ‘/’;//path or page for cookies existance
$domain = ‘.domainName.com’;//cookies domain.It can also be availabe for subdomain
$secure = true;//cookie is used for secure areas using SSH/HTTPS(boolean: true/false)
$httponly = true;//cookies can only retrived through http protocol(boolean: true/false)

Set cookie value:
Mage::getModel('core/cookie')->set($name, $value, $period, $path, $domain, $secure, $httponly);

Example:
Mage::getModel('core/cookie')->set('user_name', 'jyotiranjan', 3600, '/', true, false);
Get cookie value:
$name = Mage::getModel('core/cookie')->get($name);
Example:
$key = Mage::getModel('core/cookie')->get('user_name');
Delete cookie value:
Mage::getModel('core/cookie')->delete($name, $path, $domain, $secure, $httponly);
Example:
Mage::getModel('core/cookie')->delete('user_name', '/', '.domainName.com', true, true);

Hope you enjoyed the article Magento get set delete cookie.So please don’t forget to share or like.

Magento: get set and delete cookie
Tagged on:                         

3 thoughts on “Magento: get set and delete cookie

  • January 20, 2016 at 7:17 am
    Permalink

    I need to make feedback/survey form. In which use customers details
    fields as hidden, like name, company name, address, phone , mobile,
    city, country. and use cookies to get and send customers detail to me.

    In Result, when customer fill-up survey and submit without typing his
    name and personal information, so in email we will have all the
    information who has filled up this form.

    Is there any ways to achieve this?

    Reply
    • January 23, 2016 at 6:49 am
      Permalink

      You can set all those value with different names as cookies variables with magento’s default cookie class

      Mage.Cookies.set(‘name’, ‘jyotiranjan’);
      Mage.Cookies.set(‘company_name’, ‘jyotiranjan’);
      Mage.Cookies.set(‘phone’, ‘1234567890’);

      etc….

      and send them through form variables or get those variables wherever you want fetch by magento’s cookies class

      var name = Mage.Cookies.get(name);
      var company_name = Mage.Cookies.get(company_name);
      var phone = Mage.Cookies.get(phone);

      Reply
  • Pingback:Set, get and delete data from cookie in magento 2 - Jyotiranjan

Leave a Reply

Your email address will not be published. Required fields are marked *