简体   繁体   中英

Magento Session not working in external page (same domain)

Magento Session in external page (same domain) is not working well, I've checked all the other topics here but any solution it is working.

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

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


if($session->isLoggedIn()){
    //do it
} else {
    //  Transfer them to a login page
    header("Location: http://www.mydomain.com/customer/account/login/");
} 

I can not get this to work, I have checked all recommendations everywhere and nothing work.

I think your issue is that you already have a session started, so your attempts to start a magento session and get data from it are failing. If you look inside Mage_Core_Model_Session_Abstract_Varien, the first thing it does is look to see if the $_SESSION variable is already set and return if it is. You could set your debugger breakpoint there and verify if you aren't sure. You could close the other session and start the magento session to get your data, or you could get both to share the same session

Just for your information, after the header() function, there should be another line:

exit;

for the redirection to work.

(Apparently this belongs more to a comment rather than an answer, but I don't have the privilege to comment everwhere yet.)

You can try these lines instead, they looks similar, but work for me on Magento 1.5.1..

require_once ( "../app/Mage.php" );
umask(0);
Mage::app("default"); 
if(Mage::getSingleton('customer/session')->isLoggedIn()){
    //do it
} 
else {
    //  Transfer them to a login page
    header("Location: http://www.mydomain.com/customer/account/login/");
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM