2

I'm trying to find a way to redirect a user to the page they selected if they have been forced to log in again after a session timeout.

Right now, after the user logs in, they are redirected to index.php. But, if a user received a link in an email to a different section of my site and they have not logged in all day, they are obviously asked to log in but end up on the index.php instead of the page the link was for.

Here is a snippet of my code in the login page:

if (mysql_num_rows($result_set) == 1) {
            // username/password authenticated
            // and only 1 match
            $found_user = mysql_fetch_array($result_set);
            $_SESSION['user_id'] = $found_user['id'];
            $_SESSION['username'] = $found_user['username'];
            $_SESSION['last_activity'] = time();
            $_SESSION['time_out'] = 7200; // 2 hours
            redirect_to("index.php");

Any ideas would be helpful.

I want to thank everyone who answered my question. The solution was a combination of a few suggestions I received here. I'd like to share with you how I solved this:

Part of the reason why I was having trouble saving the url the user tried to go to before being forced to log in again was that my sessions are handled by an external php file which takes care of confirming login and expiring current session. This file is required by all pages on my website. HTTP_REFERER would not work because it would always be set to the login.php. Here's what I did on my session.php file:

session_start();

$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') 
            === FALSE ? 'http' : 'https';
$host = $_SERVER['HTTP_HOST'];
$script = $_SERVER['SCRIPT_NAME'];
$params = $_SERVER['QUERY_STRING']; 
$currentUrl = $protocol . '://' . $host . $script . '?' . $params;

if ($currentUrl != "http://domain.com/login.php?") {
    $expiryTime = time()+(60*5); // 5 mins
    setcookie('referer',$currentUrl,$expiryTime,'/');
}

Essentially, I saved the referring url in a cookie that is set to expire in 5 minutes, giving the user enough time to login. Then, on login.php I did this:

if(isset($_COOKIE['referer'])) {
                redirect_to($_COOKIE['referer']);
            } else {
                redirect_to('index.php');
            }

And, BINGO! Works every time.

  • 4
    Simply store the page they originally tried to go to and redirect to that instead. – Brad Jun 23 '13 at 07:14
  • Brad's suggestion is the standard practice. – Danny Beckett Jun 23 '13 at 07:15
  • 1
    Add a hidden input on login form with value of $_SERVER [' HTTP_REFERER'] , add a GET parameter to login url with page url they came from, add the url to a $_SESSION variable... Then redirect to that url... Some ideas... – kgarrigan Jun 23 '13 at 07:18
  • If Brad's is the standard practice I will try to follow that. Being very new to PHP I have to learn how first! Thank you guys. So many useful tips here. –  Jun 23 '13 at 07:25
  • There is a nice answer at http://stackoverflow.com/questions/14523468/redirecting-to-previous-page-after-login-php – miceno Dec 13 '13 at 16:34

3 Answers3

2

Try this:

    $actual_link = "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
    if($actual_link == 'the email link'){
        header('Location: '. $actual_link);
    }else{
        header('Location: index.php');
    }
user2067005
  • 859
  • 7
  • 15
  • Why was there a down vote? People should explain. Your suggestion looks logical. –  Jun 23 '13 at 07:27
1

Try to save the URL in session whenever a user hit any url like http://www.abc.com/profile.php once the user has successfully logged in redirect the user to saved URL(which is in session)

Moeed Farooqui
  • 3,604
  • 1
  • 18
  • 23
  • This sounds like what Brad suggested. Helps to clarify the process. Thanks, Moeed! –  Jun 23 '13 at 07:26
0

it the previous page is in the same directory and then you can try header('Location : .') or else if you if you need to redirect somewhere else.save the path before that situation occurs and in $url then u can redirect using header('Location: $url') or header('Location $url?values')

RbG
  • 3,181
  • 3
  • 32
  • 43