处理多个主机上的PHP站点的会话的最佳方法是什么? [英] What is the best way to handle sessions for a PHP site on multiple hosts?

查看:96
本文介绍了处理多个主机上的PHP站点的会话的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP将其会话信息存储在建立该会话的服务器主机的文件系统上.在多主机PHP环境中,负载在每个主机之间智能分配,PHP会话变量不适用于每个请求(除非偶然将请求分配给同一主机-假设我们无法控制负载均衡器)

PHP stores its session information on the file system of the host of the server establishing that session. In a multiple-host PHP environment, where load is unintelligently distributed amongst each host, PHP session variables are not available to each request (unless by chance the request is assigned to the same host -- assume we have no control over the load balancer).

此网站称为"The Hitchhikers Guide到PHP负载平衡" 建议重写PHP会话处理程序,并将会话信息存储在共享数据库中.

This site, dubbed "The Hitchhikers Guide to PHP Load Balancing" suggests overriding PHPs session handler and storing session information in the shared database.

您认为,在多个PHP主机环境中维护会话信息的最好的方法是什么?

What, in your humble opinion, is the best way to maintain session information in a multiple PHP host environment?

更新:非常感谢您的反馈.对于寻找示例代码的人,我们发现了我建议您检出有关为MySQL编写会话管理器类的有用教程.

UPDATE: Thanks for the great feedback. For anyone looking for example code, we found a useful tutorial on writing a Session Manager class for MySQL which I recommend checking out.

推荐答案

数据库或Database + Memcache.一般而言,会议不应该经常写.从数据库解决方案开始,该解决方案仅在会话数据已更改时才写入数据库.以后应添加Memcache,以提高性能.数据库解决方案将非常快,因为您只需要查找主键.确保数据库具有行锁定,而不具有表锁定(myISAM). MemCache仅是一个坏主意...如果它溢出,崩溃或重新启动,则用户将被注销.

Database, or Database+Memcache. Generally speaking sessions should not be written to very often. Start with a database solution that only writes to the db when the session data has changed. Memcache should be added later as a performance enhancement. A db solution will be very fast because you are only ever looking up primary keys. Make sure the db has row locking, not table locking (myISAM). MemCache only is a bad idea... If it overflows, crashes, or is restarted, the users will be logged out.

这篇关于处理多个主机上的PHP站点的会话的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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