-1

I'm trying to redirect a user back to the page he was previously on after he has logged in. So I did this in my controller:

$session = Mage::getSingleton('customer/session');
$session->setCustomerAsLoggedIn($customer);
$this->_redirectUrl($session->getBeforeAuthUrl());

However, it seems like $session->getBeforeAuthUrl() is always returning null and it ends up redirecting to an blank page.

How can I redirect a user back to the page he was previously on after he has logged in?

1

Just change the option:

System > Config > Customer 
> Costumer Redirect Customer to Account Dashboard after Logging in > NO
  • But this will affect the global behaviour, wouldn't it? I was hoping that only my module would redirect to where the user came from before logging in. – xenon May 25 '16 at 12:08
0

Try below code for the login url, it will redirect the user back to the page from where the user has clicked the login link.

Mage::helper('customer')->getLoginUrl();

and modify below settings

System -> Configuration -> Customer 
-> Costumer Redirect Customer to Account Dashboard after Logging in -> NO
  • I tried redirecting to this and I get this error localhost redirected you too many times. – xenon May 25 '16 at 4:08
  • You need not to redirect to this url, above code will generate the login url for the user and using this login user will automatically redirected to the page it was earlier. This code is not for redirection, it was for generating the login link url. – Anshu Mishra May 25 '16 at 5:12
  • Somehow, I'm getting "localhost/cms/index/noRoute" from Mage::helper('customer')->getLoginUrl(); That URL brings me to a 404. – xenon May 29 '16 at 13:30

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.