PHP 垃圾收集器不断删除我的会话

PHP Garbage Collector Keeps Removing My Sessions

问题:

我有一个网站使用 PHP 会话来允许用户登录。它工作正常。但是会话过期太快,1 分钟的不活动将使用户注销。

我的环境:

Php版本:7.1

服务器:NGINX

框架:蛋糕PHP 3.5

到目前为止我做了什么?

我在 Whosebug 或我得到的任何搜索结果上做了每一个解决方案。我在 php.ini 和 CakePHP 配置中都延长了我的会话超时时间。

解决方法

经过两三天的研究,我找到了解决办法。在我的 php.ini 中,我找到了一个名为 session.gc_probability 的配置,并为此设置了值 0。现在我的会话永远不会过期,除非用户有意注销。

现在我的问题是,我不希望我的session.gc_probability配置为零,因为它不会收集任何垃圾(对此不太确定. 如果此信息有误,请指正。)。这将导致会话保留数月或数年,这对服务器来说是真正的垃圾。

我想从 here

中给 session.gc_probability 零值
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.

我的配置究竟有什么问题?是什么导致垃圾收集很快删除我的会话? session.gc_probability 是 1,session.gc_divisor 是 1000。我认为 1/1000 概率的进程不应该每隔 1 或 2 分钟启动一次。

根据您的后续评论,您对 Session.handler 的设置是 phpSessions 文档解释:

The built-in configurations are:

  • php - Saves sessions with the standard settings in your php.ini file.
  • cake - Saves sessions as files inside tmp/sessions. This is a good option when on hosts that don’t allow you to write outside your own home dir.

[…]

session.save_path depends on your PHP distribution (and it can be changed anyway) but it normally involves a shared data storage for all PHP applications that do not opt out. That means that the app with the shortest session.gc_maxlifetime 的默认 php.ini 设置可能会从其他应用程序中删除会话数据。

切换到 cake 应该可以解决这个问题。


关于 session.gc_probabilitysession.gc_divisor 的一些跟进。将它们设置得太激进会导致频繁的垃圾收集。这可能会损害性能,但不会导致数据过早过期。 另一方面,过于宽松的值仍将允许访问过时的数据。