Home  >  Article  >  Backend Development  >  How to set session expiration time in php_PHP tutorial

How to set session expiration time in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:56:55805browse

In the apache and php environments, the default expiration time is about 20 minutes, so how do we set the session expiration? Let’s see a summary of the specific methods below.

What we most commonly use is to set it in the php program

The code is as follows
 代码如下 复制代码

if(!isset($_SESSION['last_access'])||(time()-$_SESSION['last_access'])>60)
 $_SESSION['last_access'] = time();
?>

Copy code

 代码如下 复制代码

unset($_SESSION['last_access']) 或 $_SESSION['last_access']='';
?>

if(!isset($_SESSION['last_access'])||(time()-$_SESSION['last_access'])>60) $_SESSION['last_access'] = time(); ?>


That’s it. If you want to set expiration, you can also instance it in the program

are the numerator and denominator respectively, so the probability of gc running in the session is 1%. If you modify these two values, it will reduce the efficiency of PHP. So this approach is wrong! !
The code is as follows

Copy code
 代码如下 复制代码

session.gc_maxlifetime = 86400

unset($_SESSION['last_access']) or $_SESSION['last_access']='';

?>

 代码如下 复制代码

#概率是gc_probability/gc_divisor
session.gc_probability = 1
session.gc_divisor = 100

Session has an expiration mechanism


session.gc_maxlifetime It turns out that session expiration is a small probability event. Session.gc_probability and session.gc_divisor are used to determine the probability of running gc in the session. The default values ​​of session.gc_probability and session.gc_divisor are 1 and 100 respectively

So, modifying the gc_maxlifetime variable in the php.ini file can extend the session expiration time: (for example, we modify the expiration time to 86400 seconds)
The code is as follows

Copy code
session.gc_maxlifetime = 86400 Then, just restart your web service (usually apache). When does session "recycling" occur? By default, for every php request, there will be a 1/100 probability of recycling, so it may be simply understood as "one recycling occurs for every 100 php requests." This probability is controlled by the following parameters
The code is as follows Copy code
#The probability is gc_probability/gc_divisor session.gc_probability = 1 session.gc_divisor = 100 Note 1: Assume that in this case gc_maxlifetime=120, if a session file was last modified 120 seconds ago, then the session will still be valid before the next recycling (1/100 probability) occurs. Note 2: If your session uses session.save_path to save the session elsewhere, the session recycling mechanism may not automatically process expired session files. At this time, you need to delete expired sessions manually (or crontab) regularly: cd /path/to/sessions; find -cmin +24 | xargs rm http://www.bkjia.com/PHPjc/631569.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631569.htmlTechArticleIn the environment of apache and php, the default expiration time is about 20 minutes, so how do we set the session expiration? See below for a summary of specific methods. What we most commonly use is to set it in the php program...
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