php - ini_set 'session.gc_maxlifetime' 为 1 天

标签 php session

如果我这样做:

ini_set('session.gc_maxlifetime', 86400);

这是否意味着用户可以将浏览器留在同一页面上(不活动)最多 1 天,而不必担心 session 被垃圾收集和他们被注销?

如果服务器配置不支持这个会发生什么? (它会给我一个错误吗?还是默默地失败?)

默认的垃圾收集时间是 24 分钟,这在我的系统中很容易发生。

垃圾收集是否在 session_start 上运行?

最佳答案

对于像您这样的问题,我倾向于直接查看每个 ini 设置的 PHP 手册页:http://php.net/session.gc_maxlifetime:

session.gc_maxlifetime integer

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).

  • 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.

  • Note: If you are using the default file-based session handler, your filesystem must keep track of access times (atime). Windows FAT does not so you will have to come up with another way to handle garbage collecting your session if you are stuck with a FAT filesystem or any other filesystem where atime tracking is not available. Since PHP 4.2.3 it has used mtime (modified date) instead of atime. So, you won't have problems with filesystems where atime tracking is not available.

将其映射到您的各种问题:

  • Does this mean the user can leave there browser on the same page (inactive) for up to 1 day without the worry of the session being garbage collected and them being logged out?

这仅表示 session 未被垃圾回收。如果用户由于某些其他原因(例如 cookie 丢失或某些其他 session ID 携带参数)丢失了 session ID,则无论 session 是否已被垃圾收集,用户都将自己视为已注销。

  • What happens if the server configuration doesn't support this? (Will it give me an error? or just fail silently?)

那是 PHP 配置而不是服务器配置。但是,如果您没有以默认方式使用默认 session 目录将文件放在那里,它不会给您任何错误并静默失败。负责 session 数据垃圾收集是服务器管理的职责。

如果是标准配置但没有足够的文件权限,则会触发错误。

在内部,也可以启用 session 调试,以便您可以获得有关删除了多少 session 文件的消息。

另请参阅此相关问答:

  • The default garbage collection time is 24 minutes which could happen in my system easily.

是的,默认设置是“1440”,代表 1440 秒,即 24 分钟。

您可以在此处的 PHP 源代码中找到它:

它也与 PHP 附带的建议 ini 设置使用的值相同。

另请参阅此相关问答:

  • Does garbage collection run on session_start?

是的。您可以在 session_start 实现的末尾找到它:


那么随着所有这些信息的泛滥,我们应该吸取哪些教训呢? PHP 对 session 数据进行垃圾回收,因为如果启用了 session ,将很容易产生大量数据。使用 session 保存目录的标准设置,它可以很快填满大量文件。我自己经历过这种情况,因为我在没有启动标准垃圾收集的应用程序中存在一些缺陷(我更改了 session 保存路径,因为该站点位于共享主机上并且我自己的垃圾收集没有被正确触发/配置),所以最后我所能做的就是从 shell 访问该目录并让 find 完成真正需要很长时间的工作 - 但它有效。所以手动垃圾收集。

因此,请检查您的垃圾收集设置,并在一两天后验证这些设置是否按预期工作。

除了纯垃圾回收之外,在您的应用程序中独立跟踪 session 生命周期也是明智的。这是通过将 session 创建时间和 session 上次事件时间添加到 session 中来实现的。如果您想允许长时间运行的 session 甚至强制用户在特定时间跨度后重新登录,这将允许您重新生成 session ID。

此外,对于 session 尚未被垃圾收集器删除的情况,这也可以保护您的 session 处理,因为可能没有捕获到它,或者因为它是唯一可用的 session 。由于垃圾收集器在 session 启动后启动的设计问题,垃圾收集器永远不会删除当前 session 。

我希望这能回答您的问题并为您提供一些指导。

关于php - ini_set 'session.gc_maxlifetime' 为 1 天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24337474/

相关文章:

session - 使用 Redis 存储 session 有多安全?

php - 如何更改 PHP 中的 session 超时?

php - Mysql根据一些数据创建一个表

php - mysql 语句显示表或消息

c# - 在这种情况下使用 RSA 加密是否足够安全/可能?

php - 在两个网站之间共享 session

PHP session 添加 header Pragma : no-cache

php - 如何使用mysql在两个表上应用搜索

php - 那是某种 PHP 后门吗

node.js - Node : "ENOENT" warning messages with sessions