Magento – get session in template file

sessiontemplate

I am setting a session variable on an event triggered when the url have an extra variable name "pcode" with the following code.

Mage::getSingleton('core/session')->setPcode($pcode);

Now when getting the session from one of template file with the following code..

$data=Mage::getSingleton('core/session')->getPcode();
Mage::Log($data);

*Edit:*Enable the log From back-end and checked

but I am getting nothing.

I also tried the same code by calling a model and also from calling an ajax function from the template but have the same results.

Edit:

When I am getting the session value after setting get, it retrieves the value but in another template page, its have no value.

$request = $observer->getEvent()->getData('front')->getRequest();
        $pcode  = $request->pcode;
        if(isset($pcode))
            //Mage::getModel(‘core/cookie’)->set('pcode', $pcode);
            Mage::getSingleton('core/session')->setPcode($pcode);
            $data=Mage::getSingleton('core/session')->getPcode();
                Mage::Log($data);
        exit;

Edit: Enable the log From back-end and check in var/log/system.log file your value will logged or not.

Best Answer

you can set the session using set, getting value using get and unset session using uns.

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

$myValue  =  Mage::getSingleton(‘core/session’)->getMySessionVariable();

echo $myValue;

To Unset the session

Mage::getSingleton(‘core/session’)->unsMySessionVariable();
Related Topic