Session and cookie in Magento

Magento provides very easy way to handle Cookie and session.
Magento session:
Magento session is handle by core Module ‘core/session’. Here is the way to save Session in magento

$session = Mage::getSingleton('core/session');
$session->setData('my_magento_session', array('id' => "my_session_id_value", 'setAt' => time()));

This will store a array of session “my_magento_session”.
we can get store session like this

$mysession = Mage::getSingleton('core/session')->getData('my_magento_session');
$sessionId = $mysession["id"];

and session can be removed with setting session variable null


Cookie Handling in Magento:
Setting cookie

Mage::app()->getCookie()->set('cookie_name', "cookie_value", $lifetime,$domain,$httponly);

The parameter are same as normal PHP setcookie

Getting cookie in magento:


One thought on “Session and cookie in Magento

Leave a Reply

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