9

First up, my question is very similar to questions asked in Stackoverflow (and the web) such as: How to access Magento user's session from outside Magento?

What I need is, if a customer is logged into a Magento site, I want him to be logged on to a forum too. But try as I might, I'm unable to get isLoggedIn() to be true. Any suggestions on what I might be missing? Here's the minimal code chunk that should get me logged-in information:

require_once '/abs/path/to/Mage.php';
umask(0);
Mage::app('default');
Mage::getSingleton('core/session', array('name' => 'frontend'));
$session = Mage::getSingleton('customer/session');           
Zend_Debug::dump($session->isLoggedIn());

I checked the following:

  • cookie path is set to '/'
  • I dumped the $session variable and didn't get wiser
  • As described here, I tried setting "Use Session ID in frontend", but it appears my Magento doesn't have that option (We use magento 1.3.2.4).
  • I'm checking the variable of course by logging in and out as a customer
  • Am including mage.php

Any help on what I might be missing?

Community
  • 1
  • 1
Vish
  • 2,144
  • 5
  • 25
  • 48
  • After some digging, I dumped the above contents into a file in Magento's folder, and voila, it returned isLoggedIn = true! So now I wonder, what should I do to get session information using a script outside the Magento folder? – Vish Apr 02 '12 at 19:06
  • The folders should be accesible using the same (sub)domain, are they? – erickthered Apr 02 '12 at 19:36
  • Sorry - what folders do you mean? The forums folder (where I want my checking to happen) is /var/www/testing, and the magento root is /var/www/magento. – Vish Apr 02 '12 at 19:48
  • Yeah, I meant those folders. Are they on the same (sub)domain (e.g. http://magento.mydomain.com and http://magento.mydomain.com/forums)? or they are differnt subdomains: (e.g. http://magento.mydomain.com and http://forums.mydomain.com)? – erickthered Apr 02 '12 at 20:05
  • Got you now. The end goal is to have different subdomains (as our forums will have to run in a subdomain of its own - forums.foo.com) and this is where isLoggedIn() returns false. I tried the following tests: * run above as script from magento_root: works * include above script (present in magento root) in forums code present in /var/www/testing: fails – Vish Apr 02 '12 at 20:11
  • 3
    The thing is that Magento's auth cookie (actually, any cookie) can only be read from the same (sub)domain it originated from. So if you have different (sub)domains for your store and your forum, you won't be able to read it's contents. – erickthered Apr 02 '12 at 21:04
  • Wow, that's new for me! Thanks! Could you point me to some resources that discuss this? (always a common question from a magento newbie :) ) – Vish Apr 02 '12 at 21:25
  • See also https://stackoverflow.com/a/43624081/1652951 – WackGet May 23 '19 at 14:46

1 Answers1

7

You will need to enter in .domain.com in Magentos admin->system->configuration->web->cookie domain. Like erickthered mentioned, you will need to make the cookie available to your other subdomains in order for it to be read. This is the same with all web applications.

Reference: http://www.magentocommerce.com/wiki/modules_reference/english/mage_adminhtml/system_config/edit/web#session_cookie_management_field_descriptions

B00MER
  • 5,471
  • 1
  • 24
  • 41
  • Thanks a lot B00MER! I'm now able to access my magento session cookie in a script that runs in the forums page. – Vish Apr 02 '12 at 22:13
  • 1
    @BOOMER -- All the issues I have been having for the last day were just solved. I figured, this problem was only happening on my staging site "staging.mysite.com" ... Because the cookie domain was left blank, it was reverting to "mysite.com"... Great Answer! – Zak Feb 21 '14 at 19:19