我需要为我的网站建立一个php会话到期警报/保持登录状态。 我看了几十个示例,但很困惑,回到了基础知识上。
有没有一种方法可以显示默认到期时间的倒计时?
到目前为止-为了处理这种情况-我有:
echo '<p>This is the $_SESSION[\'expiretime\']: ' . $_SESSION['expiretime'] . '</p>';
echo '<p>This is the time(): ' . time() . '</p>';
$timeleft = time() - $_SESSION['expiretime'];
echo '<p>This is the time() MINUS the $_SESSION[\'expiretime\'] : ' . $timeleft . '</p>';
我不确定参数$ _SESSION ['expiretime']是什么,但是我在线程中找到它,看起来很有趣。除了自1970年以来的秒数外,我不确定这一切告诉我什么,但可能对以后的计算有用。
答案 0 :(得分:2)
您必须做类似的事情
//Start our session.
session_start();
//Expire the session if user is inactive for 30
//minutes or more.
$expireAfter = 30;
//Check to see if our "last action" session
//variable has been set.
if(isset($_SESSION['last_action'])){
//Figure out how many seconds have passed
//since the user was last active.
$secondsInactive = time() - $_SESSION['last_action'];
//Convert our minutes into seconds.
$expireAfterSeconds = $expireAfter * 60;
//Check to see if they have been inactive for too long.
if($secondsInactive >= $expireAfterSeconds){
//User has been inactive for too long.
//Kill their session.
session_unset();
session_destroy();
} else {
echo("Expire in:");
echo($expireAfterSeconds - $secondsInactive);
}
}
$_SESSION['last_action'] = time();