Easy steps on How to Set, Retrieve and Unset Session Variables in Magento

 

Lets look out how sessions are used in Magento

Definition of Session: Sessions are the specific users data which is stored on the server for the access of datas subsequently. This will help in developing customizable applications and increase the web sites appeal.

Lets now look out how to set, unset and retrieve a magento session

‘core/session’ is the code Module that handles Magento Session.

“set” is used for setting a session variable.

Its really very simple and easy to set the session variable,

Here is the syntax followed by the example for the detailed perspection to set the session variable in the magento.

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

Example :

$sampleValue =’sample’;
Mage::getSingleton(‘core/session’)->setMyValue($sampleValue);

In the above example $sampleValue variable is declared as ‘sample’ .

The second line is used to set the variable $sampleValue in session. Now automatically the the session variable is set to magento.

Now lets see how to retrieve the session variable

‘get();’ is the code that is used to get the session variable “MyValue”.

So the syntax will be as

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

Now we are going to use ‘getMyValue();’ to get the session value of “MyValue”

So the code will be as

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

So now we have retrieved the session variable, next step is to unset the session variable. 

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

The above syntax is used to unset the session variable.

Here is an example

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

The above code will be helpful to delete the session of the variable

‘MyValue’

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()

You have to use core and customer in the frontend and adminhtml session in the backend from the above code.

Hope you got a clear idea about How to Set, Retrieve and Unset Session Variables in Magento

,kindly let us know what you think about the blog in the comment section below.

4 Responses so far.

  1. ashwin says:
    Nice post. This blog really made my work easier. I did not think it would be that much easy it would be to Set, Retrieve and Unset Session Variables in Magento. Thanks for the syntax.
  2. vijay says:
    This simple syntax and code was really usefull and it was easy to set, retrieve and unset sessions variables in magento. Thanks for the nice code with examples.
  3. Raja Parthiban says:
    can we do the same like set, retrieve and unset session variables in other websites too ??
  4. Jeni says:
    good blog to set, retrieve and unset sessions variables in magento. can i get the same for php???

LEAVE A COMMENT