PHP 会话变量的替代方案是什么?
我正在编写一个全新的网站,如果我必须在带有负载均衡器的多台计算机上托管该网站,我想确保它能够轻松扩展。
可以对网站的用户进行身份验证。换句话说,我需要维护一些状态信息。我的第一反应是使用会话变量,但随后我将被限制在一台机器上。我知道有多种方法可以将会话变量存储在外部(在数据库、redis、memcached 中),但这是唯一的选择吗?
会话变量的替代方案是什么? Facebook 和其他大型网站是如何做到这一点的?
PS 我不是在寻找另一个会话处理程序(DB、redis 等)。我想知道是否有办法完全摆脱会话变量。
I am writing a brand new website and I'd like to make sure that it scales out easily if I ever get to the point where I must host the site on multiple machines with a load balancer.
The user of the website can be authenticated. In other words, I need to maintain some state information. My first reflex was to use Session variables but then I am going to be limited to a single machine. I know that there are ways to store the session variables outside (in a DB, redis, memcached) but is that the only options?
What are the alternative to session variable? How Facebook and other big web sites are doing this?
P.S. I am not looking for another session handler (DB, redis, etc.). I'd like to know if there a way to completely get rid of session variables.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听说过 session_set_save_handler 吗?它允许您使用默认 PHP 会话处理程序(在 tmp 目录中写入 sess_xxxxxxxxxxxx 文件的处理程序)以外的机制。
您可以编写自己的使用数据库的会话处理程序。这可能是一项耗时的任务;因此您可以暂时坚持使用默认的 PHP 会话处理程序,并在准备好时透明地切换到数据库。除了实现和插入您的六个会话处理函数版本之外,您可能不需要重写任何代码。
Ever heard of session_set_save_handler? It allows you to use mechanisms other than the default PHP session handler (the one that writes sess_xxxxxxxxxxxx files in tmp directory).
You can write your own session handler that uses a database. This could be a time consuming task; so you can stick with the default PHP session handlers for the time being and transparently switch to database when you are ready. You probably won't have to rewrite any code except implementing and plugging in your version of the six session handling functions.
您可以研究缓存,即使用 Zend 缓存 或 APC 缓存。
You can look into caching, i.e using Zend cache or APC cache, for example.