Magento 客户以编程方式登录

2021-12-19 00:00:00 php magento login

我有代码:

function loginUser( $email, $password )
{
    /** @var $session Mage_Customer_Model_Session */
    $session = Mage::getSingleton( 'customer/session' );

    try
    {
        $session->login( $email, $password );
        $session->setCustomerAsLoggedIn( $session->getCustomer() );
        return 1;
    }
    catch( Exception $e )
    {
        return $e;
    }
}

$test = loginUser("login","password");
echo "test";
print_r($test);
die;

但我总是得到

Mage_Core_Exception Object ( [_messages:protected] => Array ( ) [message:protected] => 无效的登录名或密码.

登录名和密码正确,我尝试了很多帐户,结果都一样.

Login and password is correct, I tried many accounts, with the same result.

如何正确登录?

推荐答案

<?php
function loginUser( $email, $password )
    require_once ("app/Mage.php");
    umask(0);
    ob_start();
    session_start();
    Mage::app('default');
    Mage::getSingleton("core/session", array("name" => "frontend"));

    $websiteId = Mage::app()->getWebsite()->getId();
    $store = Mage::app()->getStore();
    $customer = Mage::getModel("customer/customer");
    $customer->website_id = $websiteId;
    $customer->setStore($store);
    try {
        $customer->loadByEmail($email);
        $session = Mage::getSingleton('customer/session')->setCustomerAsLoggedIn($customer);
        $session->login($email, $password);
    }catch(Exception $e){

    }


  } 
?>

相关文章