PHP 如何知道何时删除会话? [英] How does PHP know when to delete a session?

查看:42
本文介绍了PHP 如何知道何时删除会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为会话是存储在客户端的,因为浏览器关闭时会话会被​​删除.但是,今天有人告诉我这不是真的,会话存储在服务器上.

I thought that sessions were stored on the client side because of the session getting deleted when the browser closes. However, today I've been told that this is not true and that the session is stored on the server.

那么会话如何知道浏览器何时关闭,从而删除会话?

So how does a session know when a browser was closed, so that the session gets deleted?

推荐答案

没有.有两个因素在起作用:

It doesn't. There are two factors at play:

手册可能没有充分强调这一点:

The manual probably doesn't stress this enough:

这与会话的生命周期无关

无论您将此设置设置为什么,它都不会改变会话时长住在您的服务器上.

Whatever you set this setting to, it won't change how long sessions live on your server.

这只会改变 HTTP 缓存过期时间 (Expires:Cache-Control: max-age headers),它告诉浏览器它多长时间可以将页面缓存在用户的缓存中,而无需重新加载它们来自服务器.

This only changes HTTP cache expiration time (Expires: and Cache-Control: max-age headers), which advise browser for how long it can keep pages cached in user's cache without having to reload them from the server.

  • 服务端会话数据的生命周期;通过使用来自客户端的会话 ID 查找来激活"会话.它的生命周期通过此处讨论的会话垃圾收集设置控制.

  • the lifetime of the session data on the server-side; The session is "activated" via a lookup with the session ID from the client. Its lifetime is controlled via session garbage collection settings discussed here.

    session.cache_expire 文档页面上发布的评论者,大概实际上是在谈论会话数据:

    A commenter posted on the session.cache_expire documentation page, presumably actually talking about the session data:

    大多数人也不知道的是,大多数 Linux 发行版(至少对我来说 Debian 和 Ubuntu)有一个 cronbjob 来清理你的会话目录使用全局 /etc/php5/php.ini 中设置的值(其中默认为 24​​ 分钟).所以即使你在你的脚本,cronbjob 仍将使用全局价值.

    What most people also don't know, is that most Linux distributions (Debian and Ubuntu for me atleast) have a cronbjob that cleans up your session dir using the value set in the global /etc/php5/php.ini (which defaults to 24mins). So even if you set a value larger in your scripts, the cronbjob will still cleanup sessions using the global value.

    如果遇到这种情况,可以在/etc/php5/php.ini,禁用cronjob或者更好,自己做非系统范围目录或数据库中的会话清理.

    If you run into that situation, you can set the global value higher in /etc/php5/php.ini, disable the cronjob or even better, do your own session cleanup in a non-systemwide directory or a database.

    如您所见,在区分会话跟踪和会话数据存储时,社区中充满了混淆.

    As you can see, confusing abounds amongst the community when making the distinction between session tracking and session data storage.

    这篇关于PHP 如何知道何时删除会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆