Magento 1.7 Session Management – Using Mage::getSingleton

magento-1.7session

If I can use session to store a variable like this

$session = Mage::getSingleton('core/session')->setData('Message');

then what is the use of

$session = Mage::getSingleton('core/session', array('name'=>'frontend'));
what does this code actually do.

Best Answer

In a default out of the box Magento installation you have two options for the session.

  1. adminhtml - set in Mage_Adminhtml_Controller_Action
  2. frontend - set in Mage_Core_Controller_Front_Action

When the Mage_Core_Controller_Varien_Action runs it's preDispatch function it will start the session for the appropriate section.

$session = Mage::getSingleton('core/session', array('name' => $this->_sessionNamespace))->start();

The construct of the session will take in the name of the section you are in, frontend or adminhtml, and will limit the data that you can access.

public function __construct($data=array())
{
    $name = isset($data['name']) ? $data['name'] : null;
    $this->init('core', $name);
}
Related Topic