Magento logically connects to the client
I am trying to login using software in magento. I followed this tutorial .
So, I created a file called test.php in the root of my magento application.
<?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";
}
But when I access this file, it redirects me to the login form.
+3
source to share
1 answer
Try this
<?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){
}
}
?>
-2
source to share