How to use session timeout setting in php: 1. Set the [php.ini] configuration file and use the [ini_set] function to change the attribute value of the current context environment; 2. Set the Session timestamp, the code is [unset( $_SESSION['expiretime'])].
php uses session timeout setting method:
The first method, That is, set the php.ini
configuration file and set the session.gc_maxlifetime and session.cookie_lifetime
node attribute values. Of course, you can also use the ini_set
function to change them. Attribute value of the current context environment:
ini_set('session.gc_maxlifetime', "3600"); // 秒 ini_set("session.cookie_lifetime","3600"); // 秒
The second method, is to set the Session
timestamp, such as the following method.
When the login is successful, set the timestamp to 1 hour later than the current time, $_SESSION['expiretime'] = time() 3600;
. Use the following code to check the user login status:
if(isset($_SESSION['expiretime'])) { if($_SESSION['expiretime'] < time()) { unset($_SESSION['expiretime']); header('Location: logout.php?TIMEOUT'); // 登出 exit(0); } else { $_SESSION['expiretime'] = time() + 3600; // 刷新时间戳 } }
Related learning recommendations: PHP programming from entry to proficiency
The above is the detailed content of How to use session timeout setting in php?. For more information, please follow other related articles on the PHP Chinese website!