Magento Set, Retrieve and Unset Session Variables


To set a Magento session variable:

$myValue = 'Hello World';
Mage::getSingleton('core/session')->setMyValue($myValue);

To Retrieve:

$myValue = '';
$myValue=Mage::getSingleton('core/session')->getMyValue();

To Unset:

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

Note that ‘MyValue’ can be any text you want but ‘set’, ‘get’ and ‘uns’ prefixes are required.


10 responses to “Magento Set, Retrieve and Unset Session Variables”

  1. This wasn’t actually that helpful. At this point in the game I have a good handle on magic methods. But how do I mess with a session that’s not the one I’m currently in? I’m working with a swf and that doesn’t count as the same session apparently.

  2. Mage::getSingleton(‘core/session’)->setMyValue(123);

    calling the line above in a class extending Mage_Core_Block_Messages.

    Result:
    __clone method called on non-object in

    Why?!