I'm using this code to login a customer programmatically:
<?php
require_once ("app/Mage.php");
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
$email = "[email protected]";
$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>';
}
But it's not working, the result I'm getting in the browser is:
Succesfull loginById
Denied
What could be the issue here?
EDIT
Some new information: I've tried this script on a different shop we have running and it works perfectly there. Why would it work on one but not the other?
EDIT 2
More troubleshooting has resulted in the conclusion that $session->getId()
returns NULL
while it should return an actual ID. Still no idea why this shop doesn't return a session id while the other one does.
app/Mage.php
in the script.