I have Magento 2 installed in a directory at mydomain.com/shop and I want to access the user's session data outside of that folder (specifically user id and isLoggedIn) so I can use it to log them into another app. I set my Magento cookie path to "/" but what code do I need to grab the session from domain.com/file.php?


2 Answers 2


I know this post is old. However I found the way to check if a customer is logged in would be via the below code in Magento 2.1 also the file the below code is in has to reside in your Magento2 directory.

     use Magento\Framework\App\Bootstrap;

     /*** If your external file is in root folder ****/
     require __DIR__ . '/app/bootstrap.php';

     $params = $_SERVER;
     $bootstrap = Bootstrap::create(BP, $params);
     $obj = $bootstrap->getObjectManager();
     $state = $obj->get('Magento\Framework\App\State');

     $customerSession = $obj->get('Magento\Customer\Model\Session');
         echo 'Logged in';
         echo 'Not logged in';
  • This seems to be work only if the file is placed inside the Magento Root directory. Outside it may not work. Oct 20, 2017 at 18:45

Magento Customer session won't work outside the Magento. But you can trigger a login success event and set customer id to any cookie and can get anywhere in the browser. I did same since my scenario was same as mentioned by you.

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.