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.

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.

Declare Path:

Setting Domain:

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

Set Expire Time:

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:

Example:

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

Get Cookie value:

Example:

Delete Cookie Value:

Example:

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:

Example:

Get cookie value:

Example:

Delete cookie value:

Example:

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:                         

2 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

Leave a Reply

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