http://hi.baidu.com/keepnet/blog/item/ada25d1caad26d144134175c.html 单点登录是大容量系统必备的功能,市面上有几款昂贵的商业系统,若不是财大气粗,恐怕用不起。 怎么样才能简单、经济的实现这个功能?我们在这里探讨一种可行的方案。 当前开发Web应用
http://hi.baidu.com/keepnet/blog/item/ada25d1caad26d144134175c.html
单点登录是大容量系统必备的功能,市面上有几款昂贵的商业系统,若不是财大气粗,恐怕用不起。
怎么样才能简单、经济的实现这个功能?我们在这里探讨一种可行的方案。
当前开发Web应用中,Apache + PHP + MySQL是中小型企业降低成本的必选架构,这里我们来实现PHP的单点登录,让这种经济性的架构能够扩展到的群集服务器层面。
我们的设想是将PHP的Session数据集中存储,这样对于不同服务器中运行的PHP来说,只有一个共有的Session数据库,那么用户在服务器A登 录所生成的Session数据在服务器B、C、D等服务器都可以共享,就可以免除多次登录。但由于PHP的Session是需要Cookie的,而 Cookie又是与域名相关的,所以采用这个方案的各个服务器需要有相同的域名(至少是相同的二级域名),比如:
1、所有服务器的域名都是www.766.com,这个东东DNS轮询就可以实现;这个时候,在PHP中将Cookie域名设置为www.766.com即可;
2、所有服务器的域名都是以.766.com结尾的三级域名,比如a.766com,b.766.com等等,这个时候,在PHP中将Cookie域名设置为.whybsd.com就可以共享Cookie了。
解决了先决条件,我们现在来看看PHP的Session存储方法,在PHP手册说明中,有一个叫session_set_save_handler()的函数,这个函数是用来注册用户自定义的Session数据存储接口的。
以下是PHP手册自带的示例:
<span><span><span><?php <br>
</span><span>function </span><span>open</span><span>(</span><span>$save_path</span><span>, </span><span>$session_name</span></span><span><span>)<br>
{<br>
global </span><span>$sess_save_path</span></span><span><span>;<br>
</span><span>$sess_save_path </span><span>= </span><span>$save_path</span></span><span><span>;<br>
return(</span><span>true</span></span><span><span>);<br>
}<br>
<br>
function </span><span>close</span></span><span><span>()<br>
{<br>
return(</span><span>true</span></span><span><span>);<br>
}<br>
<br>
function </span><span>read</span><span>(</span><span>$id</span></span><span><span>)<br>
{<br>
global </span><span>$sess_save_path</span></span><span><span>;</span></span></span>
<span><span><span><br>
</span><span>$sess_file </span><span>= </span><span>"</span><span>$sess_save_path</span><span>/sess_</span><span>$id</span><span>"</span></span><span><span>;<br>
return (string) @</span><span>file_get_contents</span><span>(</span><span>$sess_file</span></span><span><span>);<br>
}<br>
<br>
function </span><span>write</span><span>(</span><span>$id</span><span>, </span><span>$sess_data</span></span><span><span>)<br>
{<br>
global </span><span>$sess_save_path</span></span><span><span>;<br>
<br>
</span><span>$sess_file </span><span>= </span><span>"</span><span>$sess_save_path</span><span>/sess_</span><span>$id</span><span>"</span></span><span><span>;<br>
if (</span><span>$fp </span><span>= @</span><span>fopen</span><span>(</span><span>$sess_file</span><span>, </span><span>"w"</span></span><span><span>))
{<br>
</span><span>$return </span><span>= </span><span>fwrite</span><span>(</span><span>$fp</span><span>, </span><span>$sess_data</span></span><span><span>);<br>
</span><span>fclose</span><span>(</span><span>$fp</span></span><span><span>);<br>
return </span><span>$return</span></span><span><span>;<br>
} else {<br>
return(</span><span>false</span></span><span><span>);<br>
}<br>
<br>
}<br>
<br>
function </span><span>destroy</span><span>(</span><span>$id</span></span><span><span>)<br>
{<br>
global </span><span>$sess_save_path</span></span><span><span>;<br>
<br>
</span><span>$sess_file </span><span>= </span><span>"</span><span>$sess_save_path</span><span>/sess_</span><span>$id</span><span>"</span></span><span><span>;<br>
return(@</span><span>unlink</span><span>(</span><span>$sess_file</span></span><span><span>));<br>
}<br>
<br>
function </span><span>gc</span><span>(</span><span>$maxlifetime</span></span><span><span>)<br>
{<br>
global </span><span>$sess_save_path</span></span><span><span>;<br>
<br>
foreach (</span><span>glob</span><span>(</span><span>"</span><span>$sess_save_path</span><span>/sess_*"</span><span>)
as </span><span>$filename</span></span><span><span>) {<br>
if (</span><span>filemtime</span><span>(</span><span>$filename</span><span>)
+ </span><span>$maxlifetime </span><span><span>time</span></span><span><span>())
{<br>
@</span><span>unlink</span><span>(</span><span>$filename</span></span><span><span>);<br>
}<br>
}<br>
return </span><span>true</span></span><span><span>;<br>
}<br>
<br>
</span><span>session_set_save_handler</span><span>(</span><span>"open"</span><span>, </span><span>"close"</span><span>, </span><span>"read"</span><span>, </span><span>"write"</span><span>, </span><span>"destroy"</span><span>, </span><span>"gc"</span></span><span><span>);<br>
<br>
</span><span>session_start</span></span><span><span>();<br>
<br>
</span></span><span><span>// proceed to use sessions normally<br>
<br>
</span><span>?></span></span></span></span>