0

我能够从 magento 之外的外部站点使用 magento 的登录功能。但它只有在我有用户名和密码时才有效。在某些情况下,我只有用户 ID。我尝试了 loginbyid,它返回 true 并加载客户详细信息,但是一旦将 html 加载到浏览器中,它就会删除客户会话。请注意,这不是典型的问题“从管理员登录客户”,而是使用 ID 从外部登录客户。

    require_once('path/to/Mage.php');
    umask(0);
    Mage::app('default','store', $options=null);
    Mage::getSingleton('core/session', array('name'=>'frontend'));
    $session = Mage::getSingleton('customer/session');
    $customer = $session->getCustomer();

    if($session->loginById($usernameOrId)){
        $session->setCustomerAsLoggedIn($customer);
        return $session->isLoggedIn();
    }
    return false;

这将返回 true,但在页面加载后,现在将返回 false:

    require_once('path/to/Mage.php');
    umask(0);
    Mage::app('default','store', $options=null);
    Mage::getSingleton('core/session', array('name'=>'frontend'));
    $session = Mage::getSingleton('customer/session');
    return $session->isLoggedIn();

谢谢你。

4

1 回答 1

0

要以编程方式从 Magento (Community 1.7) 外部登录客户,这是正确的代码

Mage::app('default');

// Init a Magento session. This is super ultra important
Mage::getSingleton('core/session', array('name' => 'frontend'));

// $customer Mage_Customer_Model_Customer
// We get an instance of the customer model for the actual website
$customer = Mage::getModel('customer/customer')
    ->setWebsiteId(Mage::app()->getStore()->getWebsiteId());

// Load the client with the appropriate email
$customer->loadByEmail($email);

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

// Login an check the customer by his database userId
if ($session->loginById($customer->getId())) {
    echo '<div>Succesfull loginById</div>';
} else {
    echo '<div>Error in loginById</div>';
}

if ($session->isLoggedIn()) {
    echo '<div>Welcome</div>';
} else {
    echo '<div>Denied</div>';
}
于 2013-02-25T19:17:00.877 回答