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 に答える