PHP 如何在X分钟后过期PHP会话?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 如何在X分钟后过期PHP会话?相关的知识,希望对你有一定的参考价值。

if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800)) {
    // last request was more than 30 minutes ago
    session_unset();     // unset $_SESSION variable for the run-time 
    session_destroy();   // destroy session data in storage
}
$_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp

/*
You can also use an additional time stamp to regenerate the session ID periodically to avoid attacks on sessions like session fixation:
*/
if (!isset($_SESSION['CREATED'])) {
    $_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
    // session started more than 30 minutes ago
    session_regenerate_id(true);    // change session ID for the current session an invalidate old session ID
    $_SESSION['CREATED'] = time();  // update creation time
}

//note that session.gc_maxlifetime should be at least equal to the life time of this custom expiration handler (1800 in this example).

以上是关于PHP 如何在X分钟后过期PHP会话?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 10 分钟不活动后使 PHP 会话过期? [复制]

PHP - 将会话变量设置为在 30 分钟后过期 [重复]

PHP 会话超时

PHP 会话在 40 分钟后到期

如何让 PHP 会话在浏览器关闭或一段时间后过期

关于 PHP 会话的几个问题