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.
Here I will take as example of saving email id in Magento session.
Magento: Set Session Variable
[cc lang=”php”]
$email = ‘[email protected]’;
// Set value in session
Mage::getSingleton(‘core/session’)->setEmailID($email);
[/cc]
Magento: Get Session Variable
[cc lang=”php”]
$email = ”;
$email = Mage::getSingleton(‘core/session’)->getEmailID();
[/cc]
Magento: Unset Session Variable
[cc lang=”php”]
Mage::getSingleton(‘core/session’)->unsEmailID();
[/cc]
In above three example you must be thinking that from where function name like [code]setEmailID[/code], [code]getEmailID[/code] and [code]unsEmailID[/code] are initialized? But that is the magic of PHP Magic Methods.
In Above example you will find that [code]EmailID[/code], you can use anything you want for that. It will be treated as key of your session variable, important part is [code]set[/code], [code]get[/code] and [code]uns[/code]. Those are responsible to set, get and unset the session variables in Magento.
Nice information.. Than x for providing this..!