Magento – How to set a custom Magento session variable outside of Magento

magento-1.9PHPsession

The Magento System is in "web/shop". How do I set a session variable outside of it (e.g. in "web/index.php")? And how do I read it inside of Magento (e.g. in "web/shop/app/…")?

The scenario is the following:
A user visits a landing page (e.g. "web/lp/example.php"). Dependent on the landing page, certain contents in "web/shop" must be customized only for that session.
So a session variable must be set in "web/lp/example.php" to be read later in the shop.

Thanks for your help!

Best Answer

Try to insert in your index file (outside of Magento):

require_once 'path/to/your/app/Mage.php';
Mage::app();
Mage::getSingleton('core/session', ['name' => 'frontend']);

It will give you an access to Magento core functionality like sessions etc. After that you will be able to set your custom variable

Mage::getSingleton('core/session')->setMyVariable('Some value');

Then simply call anywhere in Magento:

Mage::getSingleton('core/session')->getMyVariable();
Related Topic