Search This Blog

Sep 2, 2012

Setting, Retrieving and Unsetting Session Variables in Magento

By looking at Mage_Core_Model_Session_Abstract class in Magento, you can easily set, retrieve, and unset session variables. So if you’re planning to code related in session, use the built in code of magento instead of hardcoding it. Here are some examples:

1. To set Magento Session

$yourSessionValue = 'session Value'; 
Mage:getSingleTon('core/session')->setSessionName($yourSessionValue);

2. To retrieve Session Variables

Mage:getSingleTon('core/session')->getSessionName();

3. To unset

Mage::getSingleton('core/session')->unsSessionName();

Important: Please note that "SessionName" in unsSessionName, getSessionName, and setSessionName can be any text you want / or simply it refers to the name of the session variable. However, the prefixes 'uns', 'set', and 'get' are required.

1 comments:

Anonymous said...

Thanks dude! I'm planning to use hardcoding if I didn't saw your post. :D