我在 apache 服务器 2.4.16 上使用 PHP 5.5.38。一段时间后(大约 25 分钟),我遇到了失去 session 的问题。我在 php.ini 文件中找到了这些参数
session.gc_maxlifetime = 1440
session.gc_probability = 10
但是在代码中这些已经被设置为不同的值。代码看起来像这样
ini_set('session.gc_maxlifetime', 60*60*24*7);
ini_set('session.use_cookies', 1);
ini_set('session.gc_probability', 0);
我确认他们返回旧值,这意味着它没有失败或其他什么。
我不知道还有什么地方可以找到丢失 session 的原因。任何想法或建议将不胜感激。
最佳答案
当您使用 ini_set()
设置配置值时,它仅在当前脚本执行期间持续存在:
The configuration option will keep this new value during the script's execution, and will be restored at the script's ending.
并且,当 session.gc_maxlifetime
有多个值时,垃圾收集器将使用最低的值:
If different scripts have different values of session.gc_maxlifetime but share the same place for storing the session data then the script with the minimum value will be cleaning the data.
所以您需要做的是在您使用 session 的每个页面中分配ini_set()
,或者,更好的选择是直接修改php.ini
文件到你需要的值。
请注意,25 分钟大约是 1440 秒(24 分钟),因此它肯定使用 php.ini
的值。
关于php - 大约 25 分钟后 session 丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46198761/