For Clients
For Geeks

Access Magento session from external site

In: Magento|PHP

So, there is a simple way to access the running Magento session from outside the installation folder. The magic happens here:

1
2
3
4
5
6
7
8
9
10
11
12
$mageFilename = realpath(â??mymagentoshop/app/Mage.phpâ??);
require_once( $mageFilename );
umask(0);
 
Mage::app();
Mage::getSingleton(â??core/sessionâ??, array(â??nameâ?? => â??frontendâ??));
$session = Mage::getSingleton(â??customer/sessionâ??);
 
if($session->isLoggedIn())
   echo â??LOGGED INâ??;
else
   echo â??NOT LOGGED INâ??;

The line 5 presents the Magento initialization, and from there you can call any Magento core API functions and procedures. In order for session to work properly (as one) in both sites, you need to retrieve it, and that is exactly what is done on the lines 6 (for the core session) and 7 (for the customer session). Later in our example we’re checking whether the customer is logged in or not, but you can do whatever you want!

Oh yes, on more thing – Use SID on Frontend setting in the Magento admin (System -> Configuration -> Web -> Session Validation Settings) MUST be set to YES, otherwise it won’t work.

Written by

"Give a man a fish and you feed him for a day. Teach a man how to fish and you feed him for a lifetime." - Lao Tzu