下面詳細講到了幾點。1、點擊登錄跳轉到SSO登錄頁面並帶上當前應用的callback地址2、登錄成功後生成COOKIE並將COOKIE傳給callback地址3、callback地址接收SSO的COOKIE並設置在當前域下再跳回到應用1即完成登錄4、再在應用程序需要登錄的地方嵌入一個iframe用來實時檢測登錄狀態 代碼如下 復制代碼
<?php
//index.php 應用程序頁面
header('Content-Type:text/html; charset=utf-8');
$sso_address = 'http://2spaoku.com/sso/login.php'; //你SSO所在的域名
$callback_address = 'http://'.$_SERVER['HTTP_HOST']
.str_replace('index.php','',$_SERVER['SCRIPT_NAME'])
.'callback.php'; //callback地址用於回調設置cookie
if(isset($_COOKIE['sign'])){
exit("歡迎您{$_COOKIE['sign']} <a href="login.php?logout">退出</a>");
}else{
echo '您還未登錄 <a href="'.$sso_address.'?callback='.$callback_address.'">點此登錄</a>';
}
?>
<iframe src="<?php echo $sso_address ?>?callback=<?php echo $callback_address ?>" frameborder="0" width="0" height="0"></iframe>
<?php
//login.php SSO登錄頁面
header('Content-Type:text/html; charset=utf-8');
if(isset($_GET['logout'])){
setcookie('sign','',-300);
unset($_GET['logout']);
header('location:index.php');
}
if(isset($_POST['username']) && isset($_POST['password'])){
setcookie('sign',$_POST['username'],0,'');
header("location:".$_POST['callback']."?sign={$_POST['username']}");
}
if(empty($_COOKIE['sign'])){
?>
<form method="post">
<p>用戶名:<input type="text" name="username" /></p>
<p>密 碼:<input type="password" name="password" /></p>
<input type="hidden" name="callback" value="<?php echo $_GET['callback']; ?>" />
<input type="submit" value="登錄" />
</form>
<?php
}else{
$query = http_build_query($_COOKIE);
echo "系統檢測到您已登錄 {$_COOKIE['sign']} <a href="{$_GET['callback']}?{$query}">授權</a> <a href="?logout">退出</a>";
}
?>
<?php
//callback.php 回調頁面用來設置跨域COOKIE
header('Content-Type:text/html; charset=utf-8');
if(empty($_GET)){
exit('您還未登錄');
}else{
foreach($_GET as $key=>$val){
setcookie($key,$val,0,'');
}
header("location:index.php");
}
?>
<?php
//connect.php 用來檢測登錄狀態的頁面,內嵌在頁面的iframe中
header('Content-Type:text/html; charset=utf-8');
if(isset($_COOKIE['sign'])){
$callback = urldecode($_GET['callback']);unset($_GET['callback']);
$query = http_build_query($_COOKIE);
$callback = $callback."?{$query}";
}else{
exit;
}
?>
<html><script type="text/javascript">top.location.href="<?php echo $callback; ?>";</script></html>