How to use session timeout setting in php?

coldplay.xixi
Release: 2023-03-02 10:32:02
Original
2082 people have browsed it

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'])].

How to use session timeout setting in php?

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"); // 秒
Copy after login

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

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!

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
Popular Tutorials
More>
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!