会话变量有限制吗?
正如标题所述,会话变量是否有限制(如果有),或者它们被视为通常变量并且可以存储等量的数据?
我正在寻找除了变量类型之外是否还有其他限制,例如最大长度、最大值等。
PS 如果问题不清楚,请告诉我。
提前致谢!
As the title says, are there limits (if any) for session variables or they're considered as usual variables and can store equal amount of data?
I'm looking if there are any other limits aside from variable type ones like max length, max values and so on.
P.S. If the question is unclear, please let me know.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如@Thariama 所说,变量的数量没有限制;此外,会话中可以存储的数据量没有限制(我见过大小为数十 MB 的会话)。
随着会话的大小变大,您会遇到各种怪癖:PHP 5 在
session_start()
处将整个会话反序列化到内存中(使用默认会话处理程序 - 您可以制作您自己的解决方案);对于 20 MB 会话和 50 个并发用户,您的脚本开始受到磁盘访问速度的严重限制(又名“脚本启动慢如糖蜜”——仅会话就会占用 1 GB RAM);最后,我们专门用一个盒子在 RAM 中保存尽可能多的会话,前端盒子通过 NFS 访问它们(虽然它在我们的例子中有所帮助,但这对你来说可能有点过分了)。请注意,对于许多并发用户和磁盘上的会话存储,会话临时文件的数量可能会导致文件系统限制问题(例如,在遇到 stat() 问题之前,一个目录中可以有多少个文件) 性能),或其他限制(我们曾经发现一个盒子被配置为只允许同时打开 4096 个文件,这很困难)。这些都不是真正特定于会话的,但可以通过会话处理触发。
As @Thariama said, there's no limit on the number of variables; also, there's no limit on the amount of data you can store in a session (I've seen sessions tens of MB in size).
As the size of a session gets larger, you'll run into various quirks though: PHP 5 deserializes the whole session into memory at
session_start()
(using the default session handler - you can make you own solution, of course); with a 20 MB session and 50 concurrent users, your scripts start to be severely limited by disk access speeds (a.k.a. "script startup is slow as molasses" - the sessions alone would be hogging a GB of RAM); in the end, we dedicated a box to keep as many sessions as possible in its RAM, and the frontend boxes accessed them over NFS (although it helped in our case, this may be overkill for you).Note that for many concurrent users and session storage on disk, the number of session temporary files may cause problems with filesystem limits (e.g. how many files can be in one directory before you run into problems with
stat()
performance), or other limits (we once found the hard way that a box was configured to only allow 4096 open files at the same time). None of this is really session-specific, but can be triggered by session handling.不,会话可以拥有的空间大小(或者会话可以拥有的变量数量)没有限制。
唯一的限制是您计算机上的规格,这是由 php.ini 中的可用内存限制定义的。
请注意,该空间将在所有用户的所有会话之间共享。
No, there is no limit on much space a session may have (or how many variables a session may possess).
The only limit is the specs on your computer, this is defined by your available memory_limit in your php.ini .
Be aware that this space will be shared among all sessions for all users.
它完全特定于您的网络服务器。对于 Apache,请查看此处:
http://httpd.apache.org/docs/trunk /mod/mod_session.html
它甚至通过使用 mod_session_dbd。因此,可以克服每个会话 1 个文件等物理限制。此外,Apache 可以配置为跟踪存储在特定服务器或服务器组上的每个用户会话,以实现可扩展性。
It is completely specific to your web-server. For Apache, look here:
http://httpd.apache.org/docs/trunk/mod/mod_session.html
It even allows sessions to be stored in database by using mod_session_dbd. Therefore physical limits like 1 file per session can be overcome. Moreover, Apache can be configured to keep track of per user sessions stored on a particular server or group of servers for scalability.
简单的答案是否定的。 (也就是说,它们没有比任何其他 PHP 变量更多的限制...必须适合内存等)
但是,请记住
$_SESSION
数据默认存储为序列化的某处每个会话的一个文件中的数据。因此存在实际限制。您不想在其中存储大量信息,因为它们会从使用session_start()
的每个页面上的数据存储中加载/保存。The simple answer is no. (That is, they have no more restrictions than any other PHP variable has... must fit into memory, etc.)
However, keep in mind that
$_SESSION
data is stored somewhere, by default as serialized data in one file per session. So there are practical limitations. You wouldn't want to store a huge blob of information in them because they would be loaded/saved from the data store on every page that usessession_start()
.