1

I would like to know if it is possible to fetch the Magento customer session outside Magento and how to go about it.

For example if my Magento instance is in a folder with the path : "public_html/Magento" and if I have a php file with the path "public_html/abc.php" i.e outside the Magento folder, can I check if a user is logged in on Magento (& also get cart details) on abc.php?

I've tried various solutions out there (like including Mage.php & fetching the session), which were mostly similar to How to Create session for customer login with session outside magento
but I wasn't able to check if the customer is logged in and get the cart item details on abc.php.

4 Answers 4

2

Sounds like the cookie containing the session named frontend is not available on your abc.php script.

Make sure the cookie path in your Magento installation is set to / under System > Configuration > Web > Session Cookie Management > Cookie Path and install an extension in your browser that can easily display cookies. In chrome I use edit this cookie.

3
  • I'd already checked that. It was set to / only. But still I was unable to get the details. And / would refer to the magento root directory and not the root directory of the server... if I'm not wrong.
    – Shatir
    Oct 30, 2013 at 11:03
  • Instead of / I've set the Cookie Path to /Magento_folder_name/ and I used the method mentioned in this link stackoverflow.com/questions/15764296/… & it seems to work fine. Anyways thanks a lot for your help.
    – Shatir
    Nov 1, 2013 at 11:22
  • 1
    This worked for me. I've got a script in a sub-dir of public_html and it was creating a frontend cookie with the path set to the sub-dir. So there were then two frontend cookies but with different paths. Setting the cookie path in Magento Admin to '/' fixed this. Sep 29, 2016 at 10:53
2

Check whether the cookie frontend is passed to your abc.php. If yes, you can load the session with magento.

include Mage.php, Mage::app() and get the session Mage::getSingleton('customer/session')->start()

0

Try this code.

<?php
require_once ('app/Mage.php');
Mage::app();
// Define the path to the root of Magento installation.
define('ROOT', Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB));
$coreSession = Mage::getSingleton('core/session', array('name' => 'frontend'));
$customerSession = Mage::getSingleton("customer/session");
if($customerSession->isLoggedIn()) {
    echo "Customer is logged in";}
else{
    echo "Customer is not logged in";
}
?>

Hope this helps you!

0

In some cases you might not be sending the required session id through Cookie "frontend" as mentioned in few replies. Alternatively you can also pass on Session id through URL query parameter "SID". So if you are making a call to URL "http://yourmagentoserver/products/pots". Then your URL will be "http://yourmagentoserver/products/pots?SID=your session id"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.