Thursday, October 13

How to Access Magento session from external site

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

$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.

Hope it Helps.... Thanks....

No comments:

Post a Comment