PHP怎么样实现多服务器session共享
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">此刻</span>稍微大一点的网站基本上都有好几个子域名,<span style="color: black;">例如</span>www.motui8.cn,blog.motui8.cn,这些网站<span style="color: black;">倘若</span>需要共享用户的登录信息,就必须session共享。PHP又是<span style="color: black;">怎样</span>做到多个服务器,session共享的呢?</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">要<span style="color: black;">处理</span>这个问题<span style="color: black;">咱们</span><span style="color: black;">首要</span>要明白session的工作原理是什么?</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">session原理</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">客户端<span style="color: black;">拜访</span>PHP服务器时,执行session_start生成session_id,<span style="color: black;">通常</span><span style="color: black;">咱们</span>会把session_id存储到cookie中,<span style="color: black;">按照</span>session_id<span style="color: black;">相关</span>内容并<span style="color: black;">保留</span>在服务器文件系统中,每一次请求<span style="color: black;">经过</span>cookie获取session_id,再<span style="color: black;">经过</span>session_id获取session内容。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">然则</span>客户端<span style="color: black;">拜访</span><span style="color: black;">区别</span>服务器时,每一台服务器都会生成一个session_id,这就<span style="color: black;">引起</span><span style="color: black;">区别</span>服务器之前<span style="color: black;">没法</span>共享session了。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">处理</span>session共享其时就两个<span style="color: black;">过程</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">1. 多台服务器<span style="color: black;">针对</span>同一个客户端生成同一个session_id</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这个比较好<span style="color: black;">处理</span>,只要<span style="color: black;">咱们</span>针对主域名来生成session_id<span style="color: black;">就可</span>。实现的方式有两种:</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">1.1 <span style="color: black;">经过</span>php配置文件设置session_id的cookie域名。找到php.ini文件添加如下配置:</span></strong></p><span style="color: black;">session.cooke_domain</span> = .motui8.cn<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">1.2 <span style="color: black;">经过</span>代码设置session_id的cookie域名,代码如下:</span></strong></p><span style="color: black;">ini_set</span>( <span style="color: black;">session.cookie_domain</span>,<span style="color: black;">motui8.cn</span> );<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2. 多台服务器之前的session内容互通<span style="color: black;">有没有</span>,<span style="color: black;">能够</span><span style="color: black;">经过</span>同一个session_id获取到内容</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这一点实现起来比较麻烦,<span style="color: black;">重点</span><span style="color: black;">便是</span>把</span><strong style="color: blue;"><span style="color: black;">session</span></strong>存储在所有服务器都<span style="color: black;">能够</span><span style="color: black;">拜访</span>的<span style="color: black;">地区</span>。默认的存储在服务器的文件中,<span style="color: black;">咱们</span><span style="color: black;">亦</span><span style="color: black;">能够</span>存储在 <strong style="color: blue;"><span style="color: black;">Redis</span></strong> 这类缓存服务器中。<span style="color: black;">针对</span>文件系统<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">经过</span> <strong style="color: blue;"><span style="color: black;">NFS</span></strong>来统一存储。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.1 文件系统</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这种方式的session依赖于PHP垃圾收集器进行销毁,在高并发下session存储的目录下会产生<span style="color: black;">海量</span>的文件。这种方式会<span style="color: black;">引起</span><span style="color: black;">查询</span>文件缓慢,并且session缓存目录可存储的文件数是有限的,有可能<span style="color: black;">引起</span>session存储失败,<span style="color: black;">因此呢</span>,<span style="color: black;">咱们</span>不<span style="color: black;">意见</span><span style="color: black;">运用</span>文件系统存储session。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.2 数据库存储</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这种方式虽然不依赖PHP的垃圾收集器,<span style="color: black;">然则</span>会加大数据库的IO问题,<span style="color: black;">增多</span>数据的压力,<span style="color: black;">引起</span>数据读写速度变慢,<span style="color: black;">因此呢</span>这种方式,<span style="color: black;">咱们</span><span style="color: black;">亦</span>不<span style="color: black;">意见</span><span style="color: black;">大众</span><span style="color: black;">运用</span>。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.3 Redis 缓存存储</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">缓存数据都在内存中,读写速度快,redis还<span style="color: black;">能够</span>单独设置过期时间,自动删除无效的session。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">现有的PHP框架基本都支持Redis存储session,<span style="color: black;">咱们</span>只需要配置好session<span style="color: black;">关联</span>配置<span style="color: black;">就可</span>。当然<span style="color: black;">倘若</span>你想自己但对编写session<span style="color: black;">关联</span>代码<span style="color: black;">亦</span><span style="color: black;">能够</span>,我给<span style="color: black;">大众</span>一个示例:</span></p><span style="color: black;"><span style="color: black;"><?php</span>
<span style="color: black;">//<span style="color: black;">起步</span>session</span>
session_start();
<span style="color: black;">//设置session最大存活时间,单位【秒】</span>
ini_set(<span style="color: black;">session.gc_maxlifetime</span>,<span style="color: black;">60</span>*<span style="color: black;">60</span>);
<span style="color: black;">//设置session存储<span style="color: black;">办法</span></span>
ini_set(<span style="color: black;">session.save_handler</span>,<span style="color: black;">redis</span>);
<span style="color: black;">//设置session存储路径,存储方式为redis时,路径<span style="color: black;">便是</span>redis的连接(<span style="color: black;">倘若</span>不需要授权,直接<span style="color: black;">运用</span> tpc://127.0.0.1:6379 <span style="color: black;">就可</span>)</span>ini_set(<span style="color: black;">session.save_path</span>,<span style="color: black;">tpc://127.0.0.1:6379?persistent=1&auth=授权<span style="color: black;">秘码</span>&database=redis数据库</span>);
<span style="color: black;">//设置session内容,在内容中存储当前key的过期时间</span>
$_SESSION[<span style="color: black;">user_info</span>] = [
<span style="color: black;">data</span>=>[<span style="color: black;">id</span>=><span style="color: black;">1</span>],
<span style="color: black;">expire</span>=>time()+<span style="color: black;">60</span>*<span style="color: black;">30</span>
];
<span style="color: black;">//获取session内容</span>
$data = $_SESSION[<span style="color: black;">user_info</span>];
<span style="color: black;">//判断当前session<span style="color: black;">是不是</span>过期</span>
<span style="color: black;">if</span>($data[<span style="color: black;">expire</span>]>time()){
<span style="color: black;">//删除session中已过期的key</span>
<span style="color: black;">unset</span>($_SESSION[<span style="color: black;">user_info</span>]);
<span style="color: black;">echo</span> <span style="color: black;">session已过期</span>;
}<span style="color: black;">else</span>{
print_r($data[<span style="color: black;">data</span>]);
}</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">到此session存储的就讲完了,<span style="color: black;">期盼</span>这篇<span style="color: black;">文案</span>对<span style="color: black;">大众</span>有所<span style="color: black;">帮忙</span>。<span style="color: black;">倘若</span>有什么不对的<span style="color: black;">地区</span>,<span style="color: black;">大众</span><span style="color: black;">能够</span>在我的公众号留言。</span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p26-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/58859d38f61e4940a014d2dccc5801f1~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728208524&x-signature=SsIQ7mNHpoE%2BW5%2BqCtXdfhqzVxU%3D" style="width: 50%; margin-bottom: 20px;"></div>
“BS”(鄙视的缩写) 我完全同意你的观点,说得太对了。 我深感你的理解与共鸣,愿对话长流。 感谢你的精彩评论,为我的思绪打开了新的窗口。 论坛外链网http://www.fok120.com/
页:
[1]