问题描述
当用户点击链接并进入下一页时,它会启动一个会话,长话短说,告诉数据库使该链接不可用.在数据库重置使链接再次可用之前,他们只有 30 分钟的时间来做他们应该在这个页面上做的事情.我怎样才能让用户无法坐在并停留在页面上,保持链接不可用或点击刷新以保持在同一页面?
When a user clicks on a link and they get to the next page, it starts a session which long story short, tells the database to make that link unavailable. They only have 30 minutes to do what they are supposed to do on this page, before the database resets making the link available again.. How can I make it so that the user cannot sit and stay on the page keeping the link unavailable or clicking refresh to stay on same page?
所以基本上,有没有一种方法可以让我自动将用户重定向到另一个页面而无需他们点击任何东西?无论如何,当会话过期时,页面应该将它们重定向到另一个页面.
So basically, is there a way I can automatically redirect the user to another page WITHOUT them clicking on anything? The page should redirect them to another page when session expires no matter what.
我不认为我可以使用它因为我希望重定向依赖于会话何时到期.
header("Refresh: 60; Location: /path/somepage.php");
任何帮助都会非常有帮助!
Any help would be extremely helpful!
** 编辑,30 分钟是在会话中定义的.所以一切都与会议有关..
** EDIT, the 30 minutes is defined in the session. So its all about the session..
$now = time();
$_SESSION['start'] = time(); // taking now page start time
$_SESSION['expire'] = $_SESSION['start'] + (1 * 60) ; // ending a session in 30
$outOfTime = $_SESSION['expire'] - $_SESSION['start'];
if($now > $_SESSION['expire'])
{
header("Refresh: $outOfTime ; Location: /path/redirect.php");
}
会话计时器设置为 1 分钟以进行测试.
Session timer set to 1 minute for testing purposes.
推荐答案
因为页面的硬刷新是不可取的(对用户来说也不是很好!),你必须有 javascript 来定期查询报告页面剩余时间的侦听器,或页面到期的 unix 日期时间.
Because a hard refresh of the page is not desirable (nor is it nice for the user!), you'll have to have javascript present to periodically query a listener that reports the time remaining on the page, or the unix datetime of the page's expiry.
在受限页面的顶部:
session_start();
if (!isset($_SESSION['page_expiry']) || time() < $_SESSION['page_expiry'])
$_SESSION['page_expiry'] = time() + (60 * 30);
// render page
} else {
echo "time's up!";
}
页面内部将是 javascript,它可能每 30 秒对以下 listener.php 进行一次 ajax 调用.
Inside the page itself will be javascript that makes an ajax call to the following listener.php perhaps every thirty seconds.
session_start();
if (time() > $_SESSION['page_expiry']) echo 'false';
else echo true;
如果 ajax 调用返回 false,将它们踢出页面.
If the ajax call ever returns false, kick them out of the page.
这篇关于会话超时和自动重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!