Put data into Magento session

01.Feb.2011

Suppose you have to access your Magento session from outside the Magento system (and it’s folders), and also, you want to put some relevant data into session and retrieve it on the other side. The following procedure on the Magento side will allow you to do that:

1
Mage::getSingleton('core/session')->setMyData('exampleString');

where MyData is the name of your attribute, and you will retrieve it with getMyData. Here, you can use any name you wish, but try to avoid names which can collide with the existing API and reserved words.

On the other side you can access the session and get the data with the following code:

1
2
3
4
5
6
7
$mageFilename = realpath('MageShop/app/Mage.php');
require_once($mageFilename);
umask(0);
 
Mage::app();
$session = Mage::getSingleton('core/session', array('name' => 'frontend'));
$data = $session->getMyData(true);

On the line 1 the access to the main Magento folder is required, so here you either specify relative or absolute path to the Mage.php. The parameter true on the line 7 function call is the flag for “retrieve and remove from session” .