Showing results for 
Search instead for 
Did you mean: 

programmatically login state is logged out after redirect


programmatically login state is logged out after redirect

Manually login in frontend working well. I would like to login programmatically and automatic show the frontpage without loggin in manually. When I login programmatically the isLoggedIn state is true. When I redirect to the frontpage my current state is logged out. The session is saved in the var/session directory. Everything seems working well. I use the following redirect URL http://[storeurl]/?SID=[encryptedsessionID]

How do I get a logged in state in the frontpage? I have a default Magento installation without additional extensions. I use the following code:


$session = Mage::getSingleton( 'customer/session' );
$session->login( $email, $password );
if ($session->isLoggedIn()) {
$custSessionId = Mage::getModel("core/session")->getEncryptedSessionId();
$redirectUrl = $storeUrl . '/?SID=' . $custSessionId;


Re: programmatically login state is logged out after redirect

My approach for programmatically logging in would be use the customers email address then to use loginById as I had a problem with a customers account not so long ago which needed debugging I came up with this (I've added comments and this is an external script)

// Require our magento core
// Set our current store
// Set our session to frontend
Mage::getModel('core/session', array('name' => 'frontend'));
// load our customer object and set it to the current store
$customer = Mage::getModel("customer/customer");
// Load our customer by email
// Load the session up
$sess = Mage::getSingleton("customer/session");
// Login by ID
// Set the customer as logged in
// redirect to the homepage

 For me this returned a small visual bug of not been logged in on the homepage but this was only visual and you are actually fully logged in (this could be due to heavy caching I use or my code or something I am totally missing it was a 5 minute script anyway and did its job for me) 


Re: programmatically login state is logged out after redirect

Thank you Sven,


The problem was Magento created a new session while redirecting. I have added the following code and now it is working:

session_write_close(); // Write the data, end session
unset($_SESSION); // unset the session, magento uses isset($_SESSION) to check if it
session_name("frontend");// change the session name to frontend


Re: programmatically login state is logged out after redirect

I'm having the same issue.  I have tried adding the code mentioned in your solution to my login function however the user is still asked to login after being redirected to their dashboard.  Comparing the EncryptedSessionId before and after redirect shows that a the same session is not being used.   Any additional advice would be much appreciated.