3

I am pulling in Magento session information on a custom page using this code structure:

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

Mage::getSingleton("core/session", array("name" => "frontend"));
$session = Mage::getSingleton("customer/session"); 
$test = array();
//print_r($session);

if($session->isLoggedIn()){
    Set some session variables
} //end session check

else {
    //They don't belong here.  Transfer them to a login page
    header("Location: http://www.mydomain.com/customer/account/login/");
} 

It works great most of the time, but from time to time it doesn't seem to pull in session information sometimes. My print_r looks like this:

Mage_Customer_Model_Session Object
(
    [_customer:protected] => 
    [_isCustomerIdChecked:protected] => 
    [_skipSessionIdFlag:protected] => 
    [_data:protected] => Array
        (
            [_session_validator_data] => Array
            (
                    [remote_addr] => an.ip.addr.ess
                    [http_via] => 
                    [http_x_forwarded_for] => 
                    [http_user_agent] => Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 ( .NET CLR 3.5.30729)
                )

        [session_hosts] => Array
            (
                [www.mydomain.com] => 1
            )

    )

[_origData:protected] => 
[_idFieldName:protected] => 
[_isDeleted:protected] => 
)

But if I leave the header:location tag on, it brings me to the account page because I am logged in.

Has anyone else experienced this? How do I avoid it? I'm stumped.

4 Answers4

1

I dont know why you are using header but try using this code

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

$session = Mage::getSingleton("customer/session"); 

if($session->isLoggedIn()){
   var_dump($session->getData());
}
Kapil
  • 127
  • 1
  • 7
1

I had the same problem until I realized that I was starting a new session for my custom pages before pulling the Magento session information. When I reversed the process, it worked fine.

Pete Carter
  • 2,691
  • 3
  • 23
  • 34
Haitham
  • 11
  • 1
0

I was having this same issue and then I changed Mage::app("default"); to simply Mage::app(); and it seems to work like a charm.

todd
  • 355
  • 3
  • 11
0

In my case, it turned out that the location of the file made a lot of difference. I basically tried out Kapil's code above, with the difference that I put my file in magento root.

I wanted to add my two bits of info; hopefully this might answer your question.

EDIT: I used code similar to Kapil. If your custom page is on a separate subdomain, then you might need your cookie domain set properly in Magento Admin (please read comments on this: How to access Magento customer's session from outside Magento?)

Community
  • 1
  • 1
Vish
  • 2,144
  • 5
  • 25
  • 48