Magento external login will not create session coo

2020-05-01 10:50发布

问题:

I am trying to replace a rather clumpsy ajax-login of Magento from an external site. The site uses Magento as a shop. Both the site and the magento-shop has their own logins, therefor when a user logs in it is important that both are synchronized. This was done with an ajax-call each page reload, keeping the user logged into Magento. I want to remove this so I created a check on each page reload which will do everything server-side.

My problem is, the following code does not work properly:

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

//Check if logged in
if(!$session->isLoggedIn()){                            
    //Not logged in, therefor log in 
    $mpassword = $this->getMagentoPassword();
    $musername = $this->getAddress();   
    try
    {
        $session->login($musername, $mpassword);    
    }catch(Exception $e){
        echo $e->getMessage();
    }                            
}

Looking at cookies, there aren't any created, the ajax-login actually made a "frontend"-cookie. I know the code above actually logs in a user, but there aren't any session cookies created. Any suggestions?

回答1:

Magento only initializes the session with your code if the $_SESSION variable isn't set, i.e. session_start() wasn't called before the core session is instantiated.
Reference Mage_Core_Model_Session_Abstract_Varien::start() for details.

As a solution, the easiest way would be to start the Magento session during a request before the other session of your site is started.
Otherwise you will have to duplicate the code that sets the session name to frontend and initializes the Magento session cookie.