问题描述
我在我的网站上使用 php Sessions,它们似乎是随机消失"的.我不知道它们是否由于不活动而超时,或者我的代码是否有问题,但是有什么方法可以控制它们何时过期?
I'm using php Sessions on my website and it seems like they are "disappearing" at random intervals. I don't know if they are timing out due to inactivity or if something is wrong with my code, but is there some way to control the sessions of when they expire?
比如我可以在我的代码中添加一些东西或在 php.ini 文件中更改一些东西吗?
Like can I put something in my code or change something in the php.ini file?
更新- 所以只是在这里更新,我切换了主机,会话开始工作了.我不知道出了什么问题,但显然他们不想正常工作.
Update- So just and update here, I switched hosts and magically the sessions started working. I have no clue what was wrong but apparently they did not want to work correctly.
推荐答案
随机过期是多个应用程序共享会话数据目录的典型症状:session.gc_maxlifetime
时间最短的可能是从其他应用程序中删除数据.原因:
Random expiration is a classical symptom of session data directory shared by several applications: the one with the shortest session.gc_maxlifetime
time is likely to remove data from other applications. The reason:
- PHP 默认将会话文件存储在系统临时目录中.
内置文件处理程序不会跟踪谁拥有哪个会话文件(它只是将文件名与会话 ID 匹配):
- PHP stores session files in the system temporary directory by default.
The builtin file handler doesn't track who owns what session file (it just matches file name with session ID):
我的建议是您为您的应用程序配置一个私有的自定义会话目录.这可以通过 session_save_path()
函数 或设置 session.save_path
配置指令.有关如何在您自己的代码库中执行此操作的准确详细信息,请查看您的框架文档.
My advice is that you configure a private custom session directory for your application. That can be done with the session_save_path()
function or setting the session.save_path
configuration directive. Please check your framework's documentation for the precise details on how to do it in your own codebase.
这篇关于PHP 会话超时太快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!