在PHP中实现单点登录(Single Sign On)的简单方法
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>
具体实现可以使用多种方案,比如可以使用NFS将Session数据存储到统一的网络设备中,也可以将Session数据保存到一个数据库中,让所有服务器连接这个共享数据库(比如MySQL)就可以了。
嗯,比较简单,而且经济。
更多考虑:
1、性能需要考量,特别是服务器数(引起资源占用)和用户量(引起Session量)非常巨大的时候。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
