2013-02-05 37 views
0

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

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(); 

谢谢。

回答

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>'; 
}