JD Isaacks JD Isaacks - 1 year ago 55
PHP Question

Take user back to previous page after logging in?

I have a controller called Accounts, with the views signin and signout.

The corresponding functions look like this:

function signin()
if (!empty($this->data))
//handle login
//save login to session
$this->Session->write('Account', $data["Account"]);
//redirect to previous page

function signout()
//delete login
//redirect to previous page

If the user goes to
it first checks to see if the form is submited
if yes, it logs them in, if not it renders the signin form. If they do succesfully log in, I want to redirect them to the page they were at before the signin page.

Whats the best way to do that?


I do not think I can user a regular http referrer because technically the referrer will always be the signin in page because they go to
, then submit the sign in form. So at the point where the form is submited the referrer is always
. I want to redirect to where they were before that. Does that make sense?

Answer Source


Use a hidden <input> field that holds the initial referrer and gets submitted with the login data.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download