如果用户在 session 超时后被迫再次登录,我正在尝试找到一种方法将用户重定向到他们选择的页面。
现在,用户登录后,他们会被重定向到index.php。但是,如果用户在电子邮件中收到指向我网站不同部分的链接,并且他们一整天都没有登录,那么他们显然会被要求登录,但最终会出现在 index.php 而不是链接所在的页面上.
这是我在登录页面中的代码片段:
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");
任何想法都会有帮助。
我要感谢所有回答我问题的人。该解决方案是我在这里收到的一些建议的组合。我想与您分享我是如何解决这个问题的:
我在保存用户在被迫再次登录之前尝试访问的 url 时遇到问题的部分原因是我的 session 是由外部 php 文件处理的,该文件负责确认登录和使当前 session 过期。我网站上的所有页面都需要此文件。 HTTP_REFERER 不起作用,因为它始终设置为 login.php。这是我对 session.php 文件所做的操作:
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,'/');
}
本质上,我将引用网址保存在 cookie 中,该 cookie 设置为 5 分钟后过期,为用户提供足够的时间登录。然后,在 login.php 上我这样做了:
if(isset($_COOKIE['referer'])) {
redirect_to($_COOKIE['referer']);
} else {
redirect_to('index.php');
}
还有,宾果游戏!每次都有效。
最佳答案
试试这个:
$actual_link = "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
if($actual_link == 'the email link'){
header('Location: '. $actual_link);
}else{
header('Location: index.php');
}
关于PHP 登录后重定向到所选页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17258491/