理想的 PHP 会话大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/217420/
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
Ideal PHP Session Size?
提问by
I have a PHP form (mortgage app) that is about 400 fields, traffic on the site will be low.
我有一个大约有 400 个字段的 PHP 表单(抵押贷款应用程序),网站上的流量会很低。
What is the ideal Session size for 400 fields going into a MySQL db?
进入 MySQL 数据库的 400 个字段的理想会话大小是多少?
In php.iniwhat do I set?
php.ini我在什么地方设置?
Anything I should set that I am missing?
我应该设置什么我丢失的东西?
回答by Darryl Hein
There is no limit to the size of the session, BUT there is a limit to the memory PHP can take: http://ca.php.net/manual/en/ini.core.php#ini.memory-limit
会话的大小没有限制,但 PHP 可以占用的内存有限制:http: //ca.php.net/manual/en/ini.core.php#ini.memory-limit
回答by Darryl Hein
Well, personally I have had very large sessions before with very little problems. Probably the largest size I've had before is ~10MB. It's not optimal, but I haven't had a problem with slow scripts even with that size. I wouldn't worry about sessions getting to large, but I would try to keep it under control. My theory is, if keeping it in the session makes it much faster than quering the database every time (such as in the case of a search) then I go for it.
嗯,就我个人而言,我之前有过非常大的会议,但问题很少。可能我以前最大的大小是~10MB。这不是最佳的,但即使有这么大的大小,我也没有遇到慢速脚本的问题。我不会担心会话变大,但我会尽量控制它。我的理论是,如果将它保留在会话中比每次都查询数据库(例如在搜索的情况下)要快得多,那么我会选择它。
回答by Rajib
It's not advisable to store a lot of data in a session. As others have noted, it's not a idea to duplicate data from a database. Even if you really need some sort of caching mechanism, you wouldn't store the data in a session. The problem is that PHP rewrites the session data for every request. And as we all know writing to disk is a rather slow operation.
不建议在会话中存储大量数据。正如其他人所指出的,从数据库中复制数据不是一个想法。即使您确实需要某种缓存机制,您也不会将数据存储在会话中。问题是 PHP 会为每个请求重写会话数据。众所周知,写入磁盘是一个相当慢的操作。
If you really need to do it, serialize the array and store it in a separate file.
如果您确实需要这样做,请将数组序列化并将其存储在单独的文件中。
If you need that bunch of serialized data only 1 in 10 pages, you only unpack it once in 10 pages. Session is of course packing/unpacking for every request.
如果您只需要 10 页中的 1 个序列化数据,则只需在 10 页中解压一次。会话当然是为每个请求打包/解包。
回答by user824951
You should also check, post_max_size, max_input_vars (1000 default so you are ok)
你还应该检查,post_max_size,max_input_vars(默认为 1000,所以你没问题)

![MySQL/PHP 错误:[2002] 通常每个套接字地址(协议/网络地址/端口)只允许使用一次](/res/img/loading.gif)