说明:a.com(passport)b.com(app1) c.com(app2)
<?php
//a.com下的login.php
//主要功能:单点登陆网关,加载各应用client生成cookie,所有应用使用同一个唯一token
//主要功能:写入各应用共享数据到缓存服务器(或取得数据)
session_start();
//开始一个会话并获取唯一标志符,以session_id作为token,存储到client cookie
$sid=session_id();
//检查是否有cookie
if(!isset($_COOKIE['csid'])){
//登陆,并设置该域cookie
header(‘P3P: CP=”CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR”‘);
setcookie(‘csid’,$sid,time()+3600);
}
//加再其他域,使其生成各自域对应的cookie
echo “<script type=\”text/javascript\” src=\”http://b.com/login.php?sid=$sid\” reload=\”1\”></script>”;
echo “<script type=\”text/javascript\” src=\”http://c.com/login.php?sid=$sid\” reload=\”1\”></script>”;
if(isset($_COOKIE['csid'])){
echo $_COOKIE['csid'].’<br />’;
}
//连接memcache缓存服务器
$memcache = new Memcache;
$memcache->connect(‘localhost’, 11211) or die (“Could not connect”);
//从数据库或文件中获取的公共共享数据,比如当前用户针对所有站点的权限集
$tmp_object=new stdClass;
$tmp_object->privileges=array(“select”,”insert”);
$memcache->set(‘common_data’, $tmp_object, false, 60*60) or die (“Failed to save data at the server”);
$commondata=$memcache->get(‘common_data’);
foreach($commondata->privileges as $privilege){
echo $privilege.’<br />’;
}
?>
<?php
//b.com下的login.php
//主要功能:生成cookie
//主要功能:打开缓存服务器(或取得数据)
if(isset($_GET['sid'])){
header(‘P3P: CP=”CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR”‘);
setcookie(‘csid’,$_GET['sid'],time()+3600);
}
if(isset($_COOKIE['csid'])){
echo $_COOKIE['csid'].’<br />’;
}
//连接memcache缓存服务器
$memcache = new Memcache;
$memcache->connect(‘localhost’, 11211) or die (“Could not connect”);
//获取权限
$commondata=$memcache->get(‘common_data’);
foreach($commondata->privileges as $privilege){
echo $privilege.’<br />’;
}
?>
<?php
//c.com下的login.php
//主要功能:生成cookie
//主要功能:打开缓存服务器(或取得数据)
if(isset($_GET['sid'])){
header(‘P3P: CP=”CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR”‘);
setcookie(‘csid’,$_GET['sid'],time()+3600);
}
if(isset($_COOKIE['csid'])){
echo $_COOKIE['csid'].’<br />’;
}
//连接memcache缓存服务器
$memcache = new Memcache;
$memcache->connect(‘localhost’, 11211) or die (“Could not connect”);
//获取权限
$commondata=$memcache->get(‘common_data’);
foreach($commondata->privileges as $privilege){
echo $privilege.’<br />’;
}
?>