Use of session in Magento:
Let’s start this blog with the session definition, ‘It is a way to preserve certain data across subsequent accesses’. This enables you to build more customized applications and increase the appeal of your web site. Now, Let’s see how to set ,unset and retrieve a session in Magento.

 

Generally, Magento session is handled by core Module ‘core/session’. We use “set<session name>” to set a session variable. For more understanding, look on to the following syntax and example.

 

Mage::getSingleton(‘core/session’)->set<YOUR SESSION NAME>(<VARIABLE NAME>);
Example :
$myValue =’test’;
Mage::getSingleton(‘core/session’)->setMyValue($myValue);

 

In this example, I have declared the variable $myValue as “test”. I am going to set this variable in session. To do so, I have used the default syntax of the Magento in line 2. Now the session variable is set in the magento.Wow, its easy to set the session variable.

 

Let’s now look, how to get value from the session variable “MyValue”.  To retrieve the  session variable, we use 

 

get<YOUR SESSION NAME>();

Mage::getSingleton(‘core/session’)->get<YOUR SESSION NAME>();

 

In our example, we need to get the session value of “MyValue”. For this, let’s use “getMyValue();”. So our code will be as follows,

 

$getSession =Mage::getSingleton(‘core/session’)->getMyValue();

 

Next, we will see how to unset the session variable. To unset the session variable, we have the following syntax,

 

Mage::getSingleton(‘core/session’)->uns<YOUR SESSION NAME>();

 

Let’s have a simple example to unset the session variable.

 

Mage::getSingleton(‘core/session’)->unsMyValue();

 

This code will automatically delete the session of the MyValue.How to Use customer or core session in frontend. Use adminhtml session in the backend.



Core Session :- Mage::getSingleton(‘core/session’)
Customer Session :- Mage::getSingleton(‘customer/session’)
Admin Session :- Mage::getSingleton(‘adminhtml/session’)
Shopping Cart Session :-Mage::getSingleton(‘checkout/session’)->getQuote()