Php Magento客户以编程方式登录

Php Magento客户以编程方式登录,php,magento,login,Php,Magento,Login,我有密码: function loginUser( $email, $password ) { /** @var $session Mage_Customer_Model_Session */ $session = Mage::getSingleton( 'customer/session' ); try { $session->login( $email, $password ); $session->setCust

我有密码:

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\u Core\u异常对象([\u messages:protected]=>Array()[message:protected]=>无效的登录名或密码。

登录和密码是正确的,我尝试了许多帐户,与相同的结果


如何才能正确登录?

马丁,请尝试下面的代码

 require_once "app/Mage.php";
    Mage::app('default');
    umask(0);
    Mage::getSingleton('core/session', array('name' => 'frontend'));

    $session = Mage::getSingleton('customer/session');

$session->start();

            if (!empty($email) && !empty($password )) {
                    try {
                        $session->login($email, $password );
                        if ($session->getCustomer()->getIsJustConfirmed()) {
                            $this->_welcomeCustomer($session->getCustomer(), true);
                        }
                    } catch (Mage_Core_Exception $e) {
                        switch ($e->getCode()) {
                            case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
                                $value = Mage::helper('customer')->getEmailConfirmationUrl($email);
                                $message = Mage::helper('customer')->__('This account is not confirmed. <a href="%s">Click here</a> to resend confirmation email.', $value);
                                break;
                            case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD:
                                $message = $e->getMessage();
                                break;
                            default:
                                $message = $e->getMessage();
                        }
                        $session->addError($message);
                        $session->setUsername($email);
                    } catch (Exception $e) {
                        // Mage::logException($e); // PA DSS violation: this exception log can disclose customer password
                    }
                } else {
                    $session->addError('Login and password are required.');


   }


hi,我尝试过的thx,返回“Error”字符串。这意味着条件$customer->getId()>0返回false。脚本放在magento根目录的简单文件中,而不是在app/etc/…folderHi!我以客户身份登录,如果我注销,我会将产品添加到购物车,然后再次以来宾身份添加产品。该产品添加到以前注销的用户。您知道发生了什么吗?此代码访问magento dir ya insi的外部de magento dir.嗨!我以客户身份登录,如果我注销,我会将一个产品添加到购物车,然后再次以客户身份添加产品。该产品将添加到以前注销的用户。你知道发生了什么吗?尝试在浏览器中清除会话。你可以通过脚本中的PHP代码来完成。
try{
    $customer = Mage::getModel("customer/customer");
    $customer->setWebsiteId(Mage::app()->getWebsite()->getId());
    $customer->loadByEmail($email); //load customer by email i 
        /* if customer has ,then login */
        if($customer->getId()>0){
        $userSession = Mage::getSingleton('customer/session');
        $userSession->setCustomer($customer);
        Mage::dispatchEvent('customer_login', array('customer'=>$customer));

        }else{
            echo "Error";

        }
    }catch(Exception $e)
    {
    print_r($e->getMessage());
    }
<?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){

    }


  } 
?>