Pulling in a Magento Session Information does not work

I am collecting Magento session information on a user 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 from time to time. 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 heading: location tag, it will lead me to the account page because I have logged in.

Has anyone else experienced this? How to avoid this? I'm at a dead end.

+3
source share
4 answers

I don’t know why you are using the 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());
}
+1
source

, , , Magento. , .

+1

, Mage::app("default"); Mage::app(); , , .

0

, . Kapil , , magento.

; , .

EDIT: I used code similar to Kapil. If your user page is in a separate subdomain, then you may need your cookie domain set correctly in Magento Admin (please read the comments on this question: How to access the Magento client session from outside Magento? )

0
source

Source: https://habr.com/ru/post/1754938/


All Articles