cancel
Showing results for 
Search instead for 
Did you mean: 

programmatically login state is logged out after redirect

SOLVED

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 );
$session->setCustomerAsLoggedIn($session->getCustomer());
if ($session->isLoggedIn()) {
$custSessionId = Mage::getModel("core/session")->getEncryptedSessionId();
$redirectUrl = $storeUrl . '/?SID=' . $custSessionId;
Mage::app()->getFrontController()->getResponse()->setRedirect($redirectUrl)->sendResponse();
return;
}

3 REPLIES

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
require_once("app/Mage.php");
// Set our current store
Mage::app()->setCurrentStore(2);
// 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");
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
// Load our customer by email
$customer->loadByEmail('users@email.co.uk');
// Load the session up
$sess = Mage::getSingleton("customer/session");
// Login by ID
$sess->loginById($customer->getId());
// Set the customer as logged in
$sess->setCustomerAsLoggedIn($customer);
// redirect to the homepage
Mage::app()->getFrontController()->getResponse()->setRedirect('/')->sendResponse();

 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) 

Regards
Sven

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:


umask(0);
ob_start();
session_start();
Mage::app();
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.