set php session timeout by user

℡╲_俬逩灬. 提交于 2019-12-24 09:50:07

问题


It´s possible to set session timeout by user in php?

Example: 2 users are registred in my site. I want that each user can set their own session timeout.


回答1:


Yes, you can set a custom session timeout for each user. You can use the method as described in How do I expire a PHP session after 30 minutes? but store the absolute expiration time instead:

// set expiration time
$_SESSION['EXPIRES'] = time() + $customSessionLifetime;

// validate session
if (isset($_SESSION['EXPIRES']) && (time() < $_SESSION['EXPIRES'])) {
    // session still valid; update expiration time
    $_SESSION['EXPIRES'] = time() + $customSessionLifetime;
} else {
    // session invalid or expired
    session_destroy();
    session_unset();
}

Here $customSessionLifetime can be set differently for each user. Just make sure that its value is less than or equal to session.gc_maxlifetime and session.cookie_lifetime (if you use a cookie for the session ID).



来源:https://stackoverflow.com/questions/4596235/set-php-session-timeout-by-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!