Magento Themes and Magento Extensions

Session and cookie in Magento

Share

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

Mage::getSingleton('core/session')->setData('my_magento_session',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

Getting cookie in magento:

Mage::app()->getCookie()->get('cookie_name');

Comments are closed.