Magento 客户以编程方式登录 [英] Magento customer login programmatically

查看:35
本文介绍了Magento 客户以编程方式登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有代码:

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){

    }


  } 
?>

这篇关于Magento 客户以编程方式登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆