In this blog i will explain you how to set, retrieve and unset session variable in Magento.
Before start i would like to inform you, By default Magento equipped with three type of session:
- Core Session
- Customer Session
- Checkout Session
Core Session
Core sessions are bone of Magento, you can use it anywhere any place in the code.
Set core session: Mage::getSingelton(‘core/session’)->set
This will set the value (value_of_session) in session variable name (your_session_name).
EX:
$myValue =’test’;
Mage::getSingleton(‘core/session’)->setMyValue($myValue);
In the above example i have set the value “test” in variable myValue, and set myValue in session. Now the session “MyValue” set with value “test”.
Retrieve core session value: Mage::getSingelton(‘core/session’)->get
This will return the session variable value which you set in “your_session_name”.
Unset core session:Mage::getSingleton(‘core/session’)->uns
Same with the customer and checkout session:
Mage::getSingleton(‘customer/session’)
Mage::getSingleton(‘checkout/session’)