如何利用Magento外部的loginbyid?

时间:2013-02-05 04:31:02

标签: magento session magento-1.7

我能够从magento外部的外部网站利用magento的登录功能。但只有我有用户名和密码才有效。 有些情况下我只有userid。我尝试了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();

谢谢。

1 个答案:

答案 0 :(得分:0)

要以编程方式从Magento(社区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>';
}