JD Isaacks JD Isaacks - 15 days ago 5
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
$this->Session->delete('Account');
//redirect to previous page
???
}


If the user goes to
accounts/signin
it first checks to see if the form is submited
if(!empty($this->data))
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?

Edit:

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
/signin
, then submit the sign in form. So at the point where the form is submited the referrer is always
/signin
. I want to redirect to where they were before that. Does that make sense?

Answer

http://book.cakephp.org/view/430/referer

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

Comments