且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP会话生存期问题

更新时间:2022-06-27 00:20:05

使用 session_set_cookie_params() 更改会话cookie的生存期.请注意,默认情况下,它设置为 0 ,这意味着将设置cookie,直到用户退出浏览器为止.您可以通过以下方式执行此操作:

Use session_set_cookie_params() to change the lifetime of the session cookie. Note that by default, it is set to 0 which means that the cookie is set until the user exits the browser. You can do this in the following way:

/* Set to 0 if you want the session
   cookie to be set until the user closes
   the browser. Use time() + seconds
   otherwise. */

session_set_cookie_params(0);
session_start();

然后检查上一次活动时间,每次有人访问页面时都会更新.

Then check for the last activity time, updated each time someone visits a page.

if(($_SESSION['lastActivity'] + 300) < time()) {
    // timeout, destroy the session.
    session_destroy();
    unset($_SESSION);
    die('Timeout!');
} else {
    $_SESSION['lastActivity'] = time();
}