负载平衡集群中的PHP会话-怎么样? [英] PHP sessions in a load balancing cluster - how?

查看:70
本文介绍了负载平衡集群中的PHP会话-怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我得到了一个非常罕见的负载均衡PHP网站的独特方案.令人讨厌的是-它以前不是负载均衡的.现在我们开始遇到问题了……

OK, so I've got this totally rare an unique scenario of a load balanced PHP website. The bummer is - it didn't used to be load balanced. Now we're starting to get issues...

当前唯一的问题是PHP会话.自然,最初没有人想到此问题,因此PHP会话配置保留为默认设置.因此,两台服务器都有自己的少量会话文件存储,而麻烦的是将下一个请求引发到另一台服务器的用户,因为那没有他在第一个服务器上创建的会话.

Currently the only issue is with PHP sessions. Naturally nobody thought of this issue at first so the PHP session configuration was left at its defaults. Thus both servers have their own little stash of session files, and woe is the user who gets the next request thrown to the other server, because that doesn't have the session he created on the first one.

现在,我一直在阅读有关如何解决这种情况的PHP手册.在那里,我找到了session_set_save_handler()的漂亮功能. (而且,碰巧的是,

Now, I've been reading PHP manual on how to solve this situation. There I found the nice function of session_set_save_handler(). (And, coincidentally, this topic on SO) Neat. Except I'll have to call this function in all the pages of the website. And developers of future pages would have to remember to call it all the time as well. Feels kinda clumsy, not to mention probably violating a dozen best coding practices. It would be much nicer if I could just flip some global configuration option and Voilà - the sessions all get magically stored in a DB or a memory cache or something.

关于如何执行此操作的任何想法?


已添加:为了澄清-我希望这是标准解决方案的标准情况.仅供参考-我有一个可用的MySQL数据库.当然,必须有一些可以使用的现成代码来解决这个问题?我当然可以编写自己的会话保存内容和

Any ideas on how to do this?


Added: To clarify - I expect this to be a standard situation with a standard solution. FYI - I have a MySQL DB available. Surely there must be some ready-to-use code out there that solves this? I can, of course, write my own session saving stuff and auto_prepend option pointed out by Greg seems promising - but that would feel like reinventing the wheel. :P
Added 2: The load balancing is DNS based. I'm not sure how this works, but I guess it should be something like this.
Added 3: OK, I see that one solution is to use auto_prepend option to insert a call to session_set_save_handler() in every script and write my own DB persister, perhaps throwing in calls to memcached for better performance. Fair enough.

是否还有某种方式可以避免自己编写所有代码?像一些著名且经过测试的PHP插件吗?

Is there also some way that I could avoid coding all this myself? Like some famous and well-tested PHP plugin?

添加了很多,很晚了:这是我最终的方法:

Added much, much later: This is the way I went in the end: How to properly implement a custom session persister in PHP + MySQL?

此外,我只是在所有页面中手动添加了会话处理程序.

Also, I simply included the session handler manually in all pages.

推荐答案

您可以将PHP设置为处理数据库中的会话,因此所有服务器共享相同的会话信息,因为所有服务器为此使用相同的数据库.

You could set PHP to handle the sessions in the database, so all your servers share same session information as all servers use the same database for that.

可以在此处找到.

这篇关于负载平衡集群中的PHP会话-怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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