Home  >  Article  >  Backend Development  >  How to set session timeout in php

How to set session timeout in php

王林
王林Original
2020-08-14 13:49:553518browse

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

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[&#39;expiretime&#39;] < time()) {  
  
        unset($_SESSION[&#39;expiretime&#39;]);  
  
        header(&#39;Location: logout.php?TIMEOUT&#39;); // 登出  
  
        exit(0);  
  
    } else {  
  
        $_SESSION[&#39;expiretime&#39;] = time() + 3600; // 刷新时间戳  
  
    }  
  
}

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!

Statement:
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