Page 1 of 1

How can I redirect to previous page after login

Posted: Wed Oct 17, 2012 9:30 am
by tora0515
Title about sums it up.

Something like this

If (customer came from a different page on my site) {
redirect back to that page;
} else {
redirect as usual;
}

Any ideas on this?

Re: How can I redirect to previous page after login

Posted: Thu Oct 18, 2012 12:42 pm
by tora0515
bump

Re: How can I redirect to previous page after login

Posted: Thu Oct 18, 2012 12:44 pm
by Avvici
The first thing you do is set up the url in a session variable on the page you are trying to go to. For example if you are trying to go to your account section then in the account.php control file just add:

Code: Select all

$this->session->data['redirect'] = $this->url->link('account/account', '', 'SSL'); 
Now in the login.php control just add the redirect after a successful login. Fill it in with the session variable:

Code: Select all

$this->redirect($this->session->data['redirect']);
or if you have ampersands involved

Code: Select all

$this->redirect(str_replace('&', '&', $this->request->post['redirect'])); 

Re: How can I redirect to previous page after login

Posted: Thu Oct 18, 2012 3:25 pm
by tora0515
Since I won't know what page the user will come from, I should put

Code: Select all

$this->session->data['redirect'] = $this->url->link('account/account', '', 'SSL');
at the top of each tpl file, with account/account being changed?

Re: How can I redirect to previous page after login

Posted: Thu Oct 18, 2012 3:35 pm
by philbydevil
I would suggest going with a "header login" extension. So, the customer can be on any page and log in directly from the header, staying on the same page after login (or being redirected to another page if you wish).

I use this one:
http://www.opencart.com/index.php?route ... on_id=2609

And I use the "stay on same page or redirect to whatever" redirects from this one:
http://www.opencart.com/index.php?route ... on_id=6979

The second one has several redirects set up for different pages (homepage, login page, logout page, everything else).

Re: How can I redirect to previous page after login

Posted: Fri Oct 19, 2012 12:23 am
by Avvici
Just use the controler that was given to you to use. You might as well learn how open cart does it by default (because it happens everywhere)

Re: How can I redirect to previous page after login

Posted: Sat Jun 09, 2018 2:58 am
by imdevlper18
An extension for this on the Opencart.
https://www.opencart.com/index.php?rout ... n_id=13998

It has 3 features: Page after login. Page after logout. Plus Strict Login.