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
https://forums.phpfreaks.com/topic/158569-redirect-back-to-login/
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

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.