Jump to content

Redirect back to login


dreamwest

Recommended Posts

Im trying to send a user back to a page he was viewing before he goes to login.php page

 

page.php > login.php

 

After login redirect back to page.php

 

Ive done this code but i dont know if  $_SERVER['HTTP_REFERER'] is the best way to track internal pages

 

//after successful login redirect

$come_from = $_SERVER['HTTP_REFERER'];

if ($come_from == "site.com/login.php"){

$redirect_url = "http://site.com/index.php";

}else{

$redirect_url = "javascript:history.go(-2)";

}
redirect($redirect_url)

Link to comment
Share on other sites

Im trying to send a user back to a page he was viewing before he goes to login.php page

 

page.php > login.php

 

After login redirect back to page.php

 

Ive done this code but i dont know if  $_SERVER['HTTP_REFERER'] is the best way to track internal pages

 

//after successful login redirect

$come_from = $_SERVER['HTTP_REFERER'];

if ($come_from == "site.com/login.php"){

$redirect_url = "http://site.com/index.php";

}else{

$redirect_url = "javascript:history.go(-2)";

}
redirect($redirect_url)

 

The problem with HTTP_REFERER is that you're not guaranteed that it'll work 100% of the time (see: http://us3.php.net/manual/en/reserved.variables.server.php).  So, you essentially have two options:

 

1. Use JavaScript to go back, like you have above

2. Pass some sort of value/flag that represents the originating page to the login page via a query string or hidden field, then use that to determine what page to redirect to

Link to comment
Share on other sites

3. Just make your login a content "box" that gets included/required on any page that you need a login on. It displays the login form if you are not logged in, it submits to the current page since it is just included/required on any page so there is no need to redirect all over the place, and it displays a logout link when you are logged in.

Link to comment
Share on other sites

3. Just make your login a content "box" that gets included/required on any page that you need a login on. It displays the login form if you are not logged in, it submits to the current page since it is just included/required on any page so there is no need to redirect all over the place, and it displays a logout link when you are logged in.

 

Thats a good idea. Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.