Login Magento自动登录偶尔会失败

Login Magento自动登录偶尔会失败,login,magento-1.4,Login,Magento 1.4,我们使用下面的代码在向购物车添加产品之前自动将用户登录到Magento // This call is actually in a class called Login_model, function called dual_login // It is shown below. $lm = new Login_model(); $ret = $lm ->dual_login($Username, $Password); if ($ret['result'] = 'SUCCESS') {

我们使用下面的代码在向购物车添加产品之前自动将用户登录到Magento

// This call is actually in a class called Login_model, function called dual_login
// It is shown below.
$lm = new Login_model();
$ret = $lm ->dual_login($Username, $Password);
if ($ret['result'] = 'SUCCESS') {
    $product_id = Mage::getModel("catalog/product") -> getIdBySku("$sku");
    $product = Mage::getModel("catalog/product") -> load($product_id);          
    $session = Mage::getSingleton("core/session", array("name" => "frontend"));
    $cart = Mage::helper("checkout/cart") -> getCart();
    $cart -> addProduct($product, 1);
    $session -> setLastAddedProductId($product -> getId());
    $session -> setCartWasUpdated(true);
    $cart -> save();          
    $cart_url = $site_url_https . "store/checkout/cart";
    header("Location: " . $cart_url);
}

// 
// dual_login code below
// 
Mage::getSingleton('core/session', array('name'=>'frontend'));
$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->loadByEmail($Email);
$session = Mage::getSingleton('customer/session');
$session->login($Email,$Password);
$session->setCustomerAsLoggedIn($session->getCustomer());
// 
// How can I determine here if the login was actually successful
// and the product can be added to the cart?
// 
$ret['result'] = 'SUCCESS';
有时,下面的代码可以工作,但是产品没有被添加到购物车中,因为登录没有“粘滞”。会议似乎是个问题。如果我们让web服务器长时间不重启,Magento似乎会“忘记”它创建的会话,下面的代码会失败,因为产品没有向购物车添加任何内容

// This call is actually in a class called Login_model, function called dual_login
// It is shown below.
$lm = new Login_model();
$ret = $lm ->dual_login($Username, $Password);
if ($ret['result'] = 'SUCCESS') {
    $product_id = Mage::getModel("catalog/product") -> getIdBySku("$sku");
    $product = Mage::getModel("catalog/product") -> load($product_id);          
    $session = Mage::getSingleton("core/session", array("name" => "frontend"));
    $cart = Mage::helper("checkout/cart") -> getCart();
    $cart -> addProduct($product, 1);
    $session -> setLastAddedProductId($product -> getId());
    $session -> setCartWasUpdated(true);
    $cart -> save();          
    $cart_url = $site_url_https . "store/checkout/cart";
    header("Location: " . $cart_url);
}

// 
// dual_login code below
// 
Mage::getSingleton('core/session', array('name'=>'frontend'));
$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->loadByEmail($Email);
$session = Mage::getSingleton('customer/session');
$session->login($Email,$Password);
$session->setCustomerAsLoggedIn($session->getCustomer());
// 
// How can I determine here if the login was actually successful
// and the product can be added to the cart?
// 
$ret['result'] = 'SUCCESS';
如果我添加对$session->isLoggedIn()的调用,它将返回true,但产品仍然没有添加到购物车中


是什么导致Magento这样做?我如何测试它,以便得到通知,它正在发生?

代码中唯一的线索是您正在填充客户模型,但您没有使用它来填充会话单例。你真的应该这样做吗

$session->setCustomerAsLoggedIn($session->getCustomer());
而不是

$session->setCustomerAsLoggedIn($customer);

哦,我明白了。但这段代码已经运行了很长时间,我们从未遇到过会话导致问题的问题。当我们重新启动web服务器时,问题消失了。很明显,Magento的课程被弄糟了。不过,我会调查一下。