php 增加php会话时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5238136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Increase php session time
提问by Ken
I am trying to increase my php session time to 6 hours.
我正在尝试将我的 php 会话时间增加到 6 小时。
Here is the code to increase the session time:
这是增加会话时间的代码:
ini_set('session.gc_maxlifetime', 60 * 60 * 6); // 6 Hours
However, it seems to ONLY have a session time of 1 hour.
但是,它似乎只有 1 小时的会话时间。
Any suggestion are greatly appreciated.
任何建议都非常感谢。
Also, how do I test this feature without having to wait 6 hours to see if my session times out.
另外,我如何测试此功能而不必等待 6 小时来查看我的会话是否超时。
回答by Rohit Jindal
The scenario
场景
You're running Debian Linux or Ubuntu Linux. You want PHP sessions to last longer than the default 1440 seconds (24 minutes). So you do this:
您正在运行 Debian Linux 或 Ubuntu Linux。您希望 PHP 会话持续时间长于默认的 1440 秒(24 分钟)。所以你这样做:
ini_set('session.gc_maxlifetime', 10800); # 3 hours
With this setting, sessions should remain active for at least three hours, as long as users don't close their browser.1
使用此设置,只要用户不关闭浏览器,会话就应保持活动至少三个小时。1
But no matter what you do, sessions keep getting deleted after 24–54 minutes. It seems PHP is ignoring the gc_maxlifetime setting.
但无论您做什么,会话都会在 24-54 分钟后不断被删除。PHP 似乎忽略了 gc_maxlifetime 设置。
Why this happens
为什么会发生这种情况
Debian and Ubuntu Linux override PHP's session behavior. If you look closely, you'll see that session.gc_probability is set to 0, meaning PHP's garbage collection will never run. Instead, there's a Debian-specific cron job in /etc/cron.d/php5 that runs every 30 minutes!
Debian 和 Ubuntu Linux 会覆盖 PHP 的会话行为。如果仔细观察,您会看到 session.gc_probability 设置为 0,这意味着 PHP 的垃圾收集将永远不会运行。相反,/etc/cron.d/php5 中有一个 Debian 特定的 cron 作业,每 30 分钟运行一次!
The cron job does garbage collection based on the global session.gc_maxlifetime in php.ini. The session.gc_maxlifetime in your app is ignored.
cron 作业根据 php.ini 中的全局 session.gc_maxlifetime 进行垃圾收集。应用中的 session.gc_maxlifetime 将被忽略。
The solution
解决方案
While you could disable the cron job and/or modify php.ini, I'd prefer to fix the problem without modifying system defaults. A better solution is to create your own sessions directory, somewhere outside the normal one, and then locally enable PHP's session garbage collection.
虽然您可以禁用 cron 作业和/或修改 php.ini,但我更愿意在不修改系统默认值的情况下解决问题。更好的解决方案是创建您自己的会话目录,在正常目录之外的某个位置,然后在本地启用 PHP 的会话垃圾收集。
To do this, set session.gc_maxlifetime, session.gc_probability, session.gc_divisor, and session.save_path
:
要做到这一点,set session.gc_maxlifetime, session.gc_probability, session.gc_divisor, and session.save_path
:
# Session lifetime of 3 hours
ini_set('session.gc_maxlifetime', 10800);
# Enable session garbage collection with a 1% chance of
# running on each session_start()
ini_set('session.gc_probability', 1);
ini_set('session.gc_divisor', 100);
# Our own session save path; it must be outside the
# default system save path so Debian's cron job doesn't
# try to clean it up. The web server daemon must have
# read/write permissions to this directory.
session_save_path(APP_PARENT_DIR . '/sessions');
# Start the session
session_start();
回答by jpossi
Increasing session.gc_maxlifetime via ini_set may not work if there is another script that runs (e.g. an other vhost) that uses the same session.save_path. The other script removes the sessions of all scripts by its own lifetime:
如果有另一个使用相同 session.save_path 的脚本运行(例如其他虚拟主机),通过 ini_set 增加 session.gc_maxlifetime 可能不起作用。另一个脚本根据自己的生命周期删除所有脚本的会话:
Note:
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. In this case, use this directive together with session.save_path.
注意:
如果不同的脚本具有不同的 session.gc_maxlifetime 值,但共享相同的存储会话数据的位置,那么具有最小值的脚本将清理数据。在这种情况下,将此指令与 session.save_path 一起使用。
Source: http://php.net/manual/en/session.configuration.php#ini.session.gc-maxlifetime
来源:http: //php.net/manual/en/session.configuration.php#ini.session.gc-maxlifetime
Also be sure to change the setting before session_start(). If you have session.auto_start enabled, it is to late when you use ini_set.
还要确保在 session_start() 之前更改设置。如果您启用了 session.auto_start,那么使用 ini_set 就晚了。
回答by Jasmeen
Try following-
尝试以下-
- Avoid spaces
- 避免空格
ini_set('session.gc_maxlifetime', 60*60*6);
Or
ini_set('session.gc_maxlifetime', 60*60*6);
或者
- simply enter values into seconds as...
- 只需将值输入秒为...
ini_set('session.gc_maxlifetime', 21600);
ini_set('session.gc_maxlifetime', 21600);
回答by Mughal Sahab
use this
用这个
ini_set('session.gc_maxlifetime', 6 * 60 * 60); // 6 Hours instead to this
ini_set('session.gc_maxlifetime', 60 * 60 * 6); // 1 Hours
ini_set('session.gc_maxlifetime', [hours] * [minutes] * [seconds]);