gpt4 book ai didi

php - Magento 以编程方式登录客户

转载 作者:行者123 更新时间:2023-12-01 15:57:45 25 4
gpt4 key购买 nike

我正在尝试在 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";
}

但是当我访问这个文件时,它会将我重定向到登录表单。

最佳答案

试试这个

<?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){

}


}
?>

关于php - Magento 以编程方式登录客户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31387297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com