PHP - ini_set('session.gc_maxlifetime', 5) - Why it doesn't end the session?

后端 未结 4 361
野的像风
野的像风 2020-12-05 14:31

The PHP script is as follows:



        
4条回答
  •  执笔经年
    2020-12-05 15:21

    Read the manual (emphasis mine):

    session.gc_maxlifetime specifies the number of seconds after which data will be seen as 'garbage' and potentially cleaned up. Garbage collection may occur during session start (depending on session.gc_probability and session.gc_divisor).

    In the same page:

    session.gc_divisor coupled with session.gc_probability defines the probability that the gc (garbage collection) process is started on every session initialization. The probability is calculated by using gc_probability/gc_divisor, e.g. 1/100 means there is a 1% chance that the GC process starts on each request. session.gc_divisor defaults to 100.

    Now do the math and see that it's not very likely the GC will be called on each request.

    You should store the in the session a variable that saves the time of the last activity of the user and use that instead of the session is logically "active". Don't rely on garbage collection.

提交回复
热议问题