0

There are many snippets available to get customer login status in a script. I'm using this:

[root]/tools/testlogin.php

require_once ('../app/Mage.php');
umask(0);
Mage::app();
Mage::getSingleton('core/session', array('name'=>'frontend'));
if ($session = Mage::getSingleton('customer/session', array('name'=>'frontend'))) {
    if ($session->isLoggedIn()) {
        echo 'logged in';
    } else {
        echo 'not logged in!';
    }
} else {
    // not even a session.
    echo 'not logged in';
}

It worked a couple of times earlier, but no matter what I do, I don't get the 'logged in' message anymore. I've logged out & back in at the shop. After logout from the shop, I also noticed the 'not logged in' message appear.

I've tried clearing browser sessions, Magento cache etc. Didn't work. I've tried calling Mage::app('default');. No luck.

The only way I can make it work, 100% of the time, is by using Opera browser and putting the script not in /tools/ but in the root of the shop. For an isolated test, this is OK, but not for a complete application I'm building.

My cookie settings in Magento backoffice: (all default) Cookie path: < empty > Cookie domain: < empty >

If I specify a Cookie path: / The testlogin script doesn't even work in the root anymore.

I've also set cache headers in my script: header("Cache-Control: no-cache, must-revalidate"); //HTTP 1.1 header("Pragma: no-cache"); //HTTP 1.0 header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

I'm afraid I've run out of options. Anyone?

1

I have found a way to make it work.

Step 1: Configure in the Magento Config the Cookie Path to be / via: System > Configuration > GENERAL > Web > Session Cookie Management

Step 2: Clear all your cookies of the domain or use an 'Incognito' window. Your previous try was setting extra cookies on the subdir. This made it a mess.

Step 3: Then you can use this script in a subdir:

<?php
error_reporting( E_ALL & ~E_DEPRECATED | E_STRICT );
ini_set('display_errors', 1);

require_once ('../app/Mage.php');
umask(0);
Mage::app('default');

$coreSession = Mage::getSingleton('core/session', array('name'=>'frontend'));

if ( Mage::helper('customer')->isLoggedIn() ) {
    echo 'logged in';
} else {
    echo 'not logged in!';
}

You can check your browser cookies. If you have multiple cookies with the same name: this means trouble. Because some cookies are only valid on subdirs, strange things will happen.

  • That worked! I guess I had conflicting cookies when I tried the cookie-path "/" earlier. Thanks! – dvdmierden Oct 31 '15 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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