php 如何在多个 apache 服务器上管理单个 PHP5 会话?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6490875/
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
How to manage a single PHP5 session on multiple apache servers?
提问by learner
Hi I have to retrieve data from several web servers. First I login as a user to my web site. After successfull login I have to fetch data from different web servers and display. How can I share a single session with multiple servers. How can I achieve this?
嗨,我必须从多个 Web 服务器中检索数据。首先,我以用户身份登录到我的网站。成功登录后,我必须从不同的 Web 服务器获取数据并显示。如何与多个服务器共享单个会话。我怎样才能做到这一点?
When I first login it create session and session id saved on temp folder of that server. When I try to access another server how can I use current session that already created when I logged in. Can anybody suggest a solution?
当我第一次登录时,它会创建会话并将会话 ID 保存在该服务器的临时文件夹中。当我尝试访问另一台服务器时,如何使用登录时已创建的当前会话。有人可以提出解决方案吗?
采纳答案by Artefacto
You'll have to use another session handler.
您将不得不使用另一个会话处理程序。
You can:
你可以:
- build your own (see
session_set_save_handler
) or - use extensions that provide their own session handler, like memcached
- 构建您自己的(参见
session_set_save_handler
)或 - 使用提供自己会话处理程序的扩展,如memcached
回答by regilero
In complement to all these answers:
作为对所有这些答案的补充:
If you store sessions in databases, check that garbage collecting of sessions in PHP is really activated(it's not the case on Debian-like distributions, they decided to garbage sessions with their own cron and altered the php.ini so that it never launch any gc, so check the session.gc_probability
and session.gc_divisor
). The main problem of sessionstorage in database is that it means a lot of write queries and a lot of conflicting access in the database. This is a great way of stressing a database server like MySQL. So IMHO using another solution is better, this keeps your read/write ratio in a better web-database way.
如果您将会话存储在数据库中,请检查PHP 中会话的垃圾收集是否真的被激活(在类似 Debian 的发行版中并非如此,他们决定使用自己的 cron 对会话进行垃圾回收并更改 php.ini 以便它永远不会启动任何gc,所以检查session.gc_probability
和session.gc_divisor
)。数据库中sessionstorage的主要问题是它意味着大量的写入查询和数据库中的大量冲突访问。这是对像 MySQL 这样的数据库服务器施加压力的好方法。所以恕我直言,使用另一种解决方案更好,这可以以更好的网络数据库方式保持您的读/写比率。
You could also keep the file storage system and simply share the file directory between servers with NFS. Alter the session.save_path
setting to use something other than /tmp
. But NFS is by definition not the fastest wày of using a disk. Prefer memcached or mongodb for fast access.
您还可以保留文件存储系统,并使用NFS在服务器之间简单地共享文件目录。更改session.save_path
设置以使用除/tmp
. 但是根据定义,NFS 并不是使用磁盘的最快方式。首选 memcached 或 mongodb 以实现快速访问。
If the only thing you need to share between the server is authentification, then instead of sharing the real session storage you could share authentification credentials. Like the OpenId system in SO, it's what we call an SSO, for the web part you have several solutions, from OpenIdto CAS, and others. If the data is merged on the client side (ajax, ESI-gate) then you do not really need a common session data storage on server-side. This will avoid having 3 of your 5 impacted web application writing data in the shared session in the same time. Other session sharing techniques (database, NFS, even memcached) are mostly used to share your data between several servers because Load Balancing tools can put your sequential HTTP request from one server to another, but if you really mean parallel gathering of data you should really study SSO.
如果您需要在服务器之间共享的唯一内容是身份验证,那么您可以共享身份验证凭据而不是共享真实的会话存储。就像 SO 中的 OpenId 系统一样,这就是我们所说的 SSO,对于 Web 部件,您有多种解决方案,从OpenId到CAS, 和别的。如果数据在客户端(ajax、ESI-gate)合并,那么你真的不需要服务器端的公共会话数据存储。这将避免 5 个受影响的 Web 应用程序中的 3 个同时在共享会话中写入数据。其他会话共享技术(数据库、NFS,甚至 memcached)主要用于在多台服务器之间共享您的数据,因为负载平衡工具可以将您的顺序 HTTP 请求从一台服务器发送到另一台服务器,但如果您真的意味着并行收集数据,您应该真的学习单点登录。
回答by chiborg
Another option would be to use memcachedto store the sessions.
另一种选择是使用memcached来存储会话。
The important thing is that you must have a shared resource - be it a SQL database, memcached, a NoSQL database, etc. that all servers can access. You then use session_set_save_handlerto access the shared resource.
重要的是你必须有一个共享资源——无论是 SQL 数据库、memcached、NoSQL 数据库等,所有服务器都可以访问。然后使用session_set_save_handler访问共享资源。
回答by Bjorn
Store sessions in a database which is accessible from the whole server pool.
将会话存储在可从整个服务器池访问的数据库中。
回答by tucuxi
Store it in a database - get all servers to connect to that same database. First result for "php store session in database"
将其存储在数据库中 - 让所有服务器连接到同一个数据库。对于“第一个结果数据库PHP存储会话”