在多个主机上处理 PHP 站点会话的最佳方法是什么?

编程入门 行业动态 更新时间:2024-10-23 02:05:04
本文介绍了在多个主机上处理 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).

这个网站,被称为搭便车指南"到 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,或 Database+Memcache.一般来说,会话​​不应经常写入.从仅在会话数据发生更改时写入数据库的数据库解决方案开始.稍后应添加 Memcache 作为性能增强.db 解决方案将非常快,因为您只需要查找主键.确保数据库具有行锁定,而不是表锁定 (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 站点会话的最佳方法是什么?

本文发布于:2023-07-17 06:44:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1130511.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   主机   站点   方法   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!