Magento: Set, Get and Unset Session Variable

  • Avinash
  • 1
  • May 15, 2013
  • Web Development

In this quick article we will see how we can deal with Session variable in Magento. Here dealing include set new variable, get value of existing variable and deleting existing variable.

Dealing with Magento Session

Here I will take as example of saving email id in Magento session.

Magento: Set Session Variable

$email = '[email protected]'; // Set value in session Mage::getSingleton('core/session')->setEmailID($email);
$email = '[email protected]';

// Set value in session
Mage::getSingleton('core/session')->setEmailID($email);

Magento: Get Session Variable

$email = ''; $email = Mage::getSingleton('core/session')->getEmailID();
$email = '';
$email = Mage::getSingleton('core/session')->getEmailID();

Magento: Unset Session Variable

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

In above three example you must be thinking that from where function name like setEmailID, getEmailID and unsEmailID are initialized? But that is the magic of PHP Magic Methods.

In Above example you will find that EmailID, you can use anything you want for that. It will be treated as key of your session variable, important part is set, get and uns. Those are responsible to set, get and unset the session variables in Magento.

Related Posts

Written by Avinash

Avinash Zala currently working at a leading web development company as a Sr. PHP Web Developer. He is the founder of the Xpert Developer. He has completed B.E. in Information Technology. Born and brought up in Ahmedabad. He loves to code and coding is all about passion for him. Follow him on twitter at Avinash Zala & Xpert Developer.

View all posts by:

  • pavan bangaram

    Nice information.. Than x for providing this..!