Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access Magento user's session from outside Magento?

Im trying to access the Magento customer session in another part of my website.

domain.com/shop/ <- Magento
domain.com/test.php

The shop itself works like a charm, however im trying to determine within test.php if a customer is logged in, so I can display his name with a link to his cart.

Contents of test.php so far:

<?php
require_once dirname(__FILE__).'/shop/app/Mage.php';
umask(0);
Mage::app('default');
Mage::getSingleton('core/session', array('name' => 'frontend'));
var_dump(Mage::getSingleton('customer/session')->isLoggedIn());
?>

I keep getting bool(false) returned. I'm logged into Magento at domain.com/shop/ so ‘true’ is expected.

Am I forgetting something?

like image 488
Martijn Heemels Avatar asked Jul 08 '09 15:07

Martijn Heemels


People also ask

How do I login as a Customer in Magento 2?

On the Admin sidebar go to Stores > Settings > Configuration. In the left panel, expand Customers and choose Login as Customer. Set Enable Login as Customer to Yes .

Where are Magento 2 sessions stored?

Session's are stored in Redis server. We can also configure the Redis cache from root/app/env. php file in Magento2.

How does Magento session work?

To manage sessions, Magento uses the Magento \ Framework \ Session \ SessionManager class. Magento also stores the current session identifier in cookies, which helps the system to identify you as a logged in user, even after you close and open the browser.


1 Answers

I would recommend checking the path of the cookie that Magento sets. Most likely, it is set to /shop, so the cookie will not be accessible to your file above it.

You can modify the path that Magento uses to set its cookie in the control panel under System -> Configuration -> Web (under the General heading) -> Session cookie management

like image 111
jason Avatar answered Oct 28 '22 15:10

jason