PHP会话难于在分布式系统上扩展吗? [英] Are PHP sessions hard to scale across a distributed system?

查看:69
本文介绍了PHP会话难于在分布式系统上扩展吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在工作中,我们几乎用Java和perl进行所有操作,但是我想使用PHP和会话构建功能。一些窥视者认为尝试在我们的系统上进行PHP会话是一个坏主意,因为它已分发到许多服务器上。具体问题是什么?

解决方案

您的具体问题的答案是什么,在于以下事实:默认情况下,PHP将其会话存储在文件系统上的文件中。对于单个服务于请求的Web服务器,这不是问题,因为您的会话数据将始终可用。但是,如果您有两个负载平衡的Web服务器来处理请求,那么该怎么办?然后,您的下一个请求到达第二台Web服务器。当然,第二个Web服务器将看不到会话文件。对于用户来说,您可能登录到一个网站,然后突然注销。



这不是特定于PHP的问题,非常普遍。解决方案是将会话数据存储在某些公共区域中。最常见的方法是将会话数据存储在所有Web服务器都可以访问的数据库中,或者存储在诸如memcached之类的共享内存缓存服务器中。


At work we do almost everything in Java and perl, but I wanted to build out a feature using PHP and sessions. Some peeps thought that it was a bad idea to try to do PHP sessions on our system, cause it's distributed to many servers. What would the specific problem be?

解决方案

The answer to your specific question, what would the problem be, lies in the fact that by default PHP stores its sessions in files on the filesystem. For a single webserver serving requests, this is not a problem because your session data will always be available. But what if you had two load-balanced webservers serving requests?

Imagine hitting the first webserver with a request, which creates your session file on its file system. Then, your next request hits the second webserver. The second webserver will of course not see the session file. To the user, you might log in to a website, and then suddenly be logged out.

This is not a problem specific to PHP, and is very common. The solution is to store session data in some common area. The most common method for this is to store session data either in a database accessible to all web servers, or some shared memory cache server like memcached.

这篇关于PHP会话难于在分布式系统上扩展吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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