检查 php 会话到期前剩余的时间
Check time left before php session expires
我需要为我的网站构建一个 php 会话到期警报/保持登录提示。
我看过几十个例子,但很困惑,所以回到基础。
有没有办法显示默认到期时间的倒计时?
更新 1
到目前为止 - 为了了解情况 - 我有:
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 年以来的秒数,我不确定所有这些告诉我什么,但可能对后续计算有用。
你必须做类似
的事情
//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();
我需要为我的网站构建一个 php 会话到期警报/保持登录提示。 我看过几十个例子,但很困惑,所以回到基础。
有没有办法显示默认到期时间的倒计时?
更新 1
到目前为止 - 为了了解情况 - 我有:
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 年以来的秒数,我不确定所有这些告诉我什么,但可能对后续计算有用。
你必须做类似
的事情//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();