我正在尝试以magento的方式以编程方式登录客户。 我跟着this tutorial。
所以我在magento应用程序的根目录中创建了一个名为test.php的文件。
<?php
require_once 'app/Mage.php';
function loginById($customerId)
{
Mage::app(); // to make sure that Mage::getModel() works
$customer = Mage::getModel('customer/customer')->load($customerId);
if ($customer->getWebsiteId()) {
Mage::init($customer->getWebsiteId(), 'website');
$session = Mage::getSingleton('customer/session');
$session->loginById($customerId);
return $session;
}
throw new Exception('Login failed');
}
try {
$session = loginById(1);
//echo Mage_Customer_Model_Session::isLoggedIn();exit;
// var_dump($session);exit;
// function login() from above
$session->setAfterAuthUrl(
'http://192.168.1.61/magento/index.php/customer/account');
//echo $session->getAfterAuthUrl();exit;
header('Location: ' . $session->getAfterAuthUrl());
} catch (Mage_Core_Exception $e) {
echo "error";
}
但是当我访问此文件时,它会将我重定向到登录表单。
答案 0 :(得分:-2)
试试这个
<?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){
}
}
?>