How to set session timeout in php

王林
Release: 2023-03-04 13:08:02
Original
3432 people have browsed it

How to set the session timeout in php: You can use the ini_set() function to change the attribute value of the current context to achieve the purpose of setting the session timeout, such as: [ini_set('session.gc_maxlifetime', " 3600");].

How to set session timeout in php

First method:

(Recommended tutorial:php graphic tutorial)

Set the php.ini configuration file, set the session.gc_maxlifetime and session.cookie_lifetime node attribute values. Of course, you can also use the ini_set function to change the attribute values of the current context environment:

ini_set('session.gc_maxlifetime', "3600"); // 秒 ini_set("session.cookie_lifetime","3600"); // 秒
Copy after login

Second method:

Set Session timestamp

When the login is successful, set the timestamp to 1 hour after the current time, $_SESSION['expiretime'] = time() 3600;.

(Video tutorial recommendation:php video tutorial)

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; // 刷新时间戳 } }
Copy after login

The above is the detailed content of How to set session timeout in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!