Set Get and Delete cookie in Magento Server Side.

Set Get and Delete cookie in Magento Server Side.
Facebooktwitterredditpinterestlinkedinmail

Set Get and Delete cookie in Magento. Setting cookie Server Side :
Mage_Core_Model_Cookie class contains function to Set, Get and delete cookies in magento.

$name : Name of the cookie.
$value : value of the cookie.
$period : expiry time of the cookie.
$path : path of the server on which cookie is available if set ‘/’ the cookie is availble entire domain ‘/directoryname/’ this means cookie available directory and all sub directory.
$domain : The doamin the cookie is available ‘www.example.com’;
$secure : Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client. it is boolean that is TRUE or FALSE.
$httponly : When TRUE the cookie will be made accessible only through the HTTP protocol.

$name and $value are mandatory while setting cookies and other parameters are not mandatory.

Setting cookie :

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

Getting cookie :

Only pass cookie name in $name variable to get cookie.

<?php
Mage::getModel('core/cookie')->get($name);
?>

Get all cookie value as array :

<?php
Mage::getModel('core/cookie')->get();
?>

Deleting cookie :

Only $name is mandatory and other variable are optional.

<?php
Mage::getModel('core/cookie')->delete($name, $path, $domain, $secure, $httponly);
?>

Example :

<?
$name = 'name';
$value = 'Aman';
$period = time()+60*60*24*30; //expiration time is set to a month (60 sec * 60 min * 24 hours * 30 
days);
$path = '/';
$domain = 'www.example.com';
$secure = false;
$httponly = false;
Mage::getModel('core/cookie')->set($name, $value, $period, $path, $domain, $secure, $httponly);
?>

OR

<?php
$name = 'name';
$value = 'Aman';
Mage::getModel('core/cookie')->set($name, $value);
echo Mage::getModel('core/cookie')->get('name');
//it print value of set cookie. 
?>

Deleting Cookie:

<?php
Mage::getModel('core/cookie')->delete('name');
?>
Facebooktwitterredditpinterestlinkedinmail