1

I'm working on a script that will allow me to log in as a specific customer programmatically, then redirect me to the main store page. I've tried a number of things, but none of them have worked.

The Mage file is correct and I'm not getting any exceptions. On redirect, I am simply not logged in. Removing the redirect, letting the script run and then navigating manually to the site does not keep me logged in.

Here's what I've tried:

require_once('../app/Mage.php');
Mage::setIsDeveloperMode(true);
Mage::app("default");
try{

Mage::getSingleton('core/session', array('name' => 'frontend'));
$customer = Mage::getModel("customer/customer");
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());

$customer->loadByEmail('nsteuver@sportys.com');
$session = Mage::getSingleton("customer/session");
$session->loginById($customer->getId());
$session->setCustomerAsLoggedIn($customer);
}
catch(Exception $e) { echo $e; }
$url = 'http://mystore.com';
header('Location: '. $url);

require_once('../app/Mage.php');
Mage::setIsDeveloperMode(true);
Mage::app("default");
try{

$customer = Mage::getModel('customer/customer')
->setWebsiteId(Mage::app()->getStore()->getWebsiteId())
->loadByEmail($email);

Mage::getSingleton('customer/session')->setCustomerAsLoggedIn($customer)
->renewSession();
}
catch(Exception $e) { echo $e; }
$url = 'http://mystore.com';
header('Location: '. $url);

require_once('../app/Mage.php');
Mage::setIsDeveloperMode(true);
Mage::app("default");
try{

function loginId($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');
 }

    loginId(274660); //This is my test customer Id
}
catch(Exception $e) { echo $e; }
$url = 'http://mystore.com';
header('Location: '. $url);

I've tried various permutations of these, as well as replacing Mage::app("default") with Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID)) and have found no difference. The behavior is exactly the same. Adding exit() after the redirect stops the redirect but has no effect on retaining the session, just like leaving the redirect off entirely.

Can anyone point me in the right direction?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.