欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php的sso單點(diǎn)登錄實(shí)現(xiàn)方法

 更新時(shí)間:2015年01月08日 08:53:00   投稿:shichen2014  
這篇文章主要介紹了php的sso單點(diǎn)登錄實(shí)現(xiàn)方法,實(shí)例分析了sso單點(diǎn)登錄的原理與具體實(shí)施步驟,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了php的sso單點(diǎn)登錄實(shí)現(xiàn)方法。分享給大家供大家參考。具體分析如下:

這里詳細(xì)講到了幾點(diǎn):
1、點(diǎn)擊登錄跳轉(zhuǎn)到SSO登錄頁(yè)面并帶上當(dāng)前應(yīng)用的callback地址
2、登錄成功后生成COOKIE并將COOKIE傳給callback地址
3、callback地址接收SSO的COOKIE并設(shè)置在當(dāng)前域下再跳回到應(yīng)用1即完成登錄
4、再在應(yīng)用程序需要登錄的地方嵌入一個(gè)iframe用來(lái)實(shí)時(shí)檢測(cè)登錄狀態(tài),代碼如下:
index.php 應(yīng)用程序頁(yè)面:

復(fù)制代碼 代碼如下:
<?php 
header('Content-Type:text/html; charset=utf-8'); 
$sso_address = 'http://XXXX.com/sso/login.php'; //你SSO所在的域名 
$callback_address = 'http://'.$_SERVER['HTTP_HOST'] 
                    .str_replace('index.php','',$_SERVER['SCRIPT_NAME']) 
                    .'callback.php'; //callback地址用于回調(diào)設(shè)置cookie
 
if(isset($_COOKIE['sign'])){ 
    exit("歡迎您{$_COOKIE['sign']} <a href="login.php?logout">退出</a>"); 
}else{ 
    echo '您還未登錄 <a href="'.$sso_address.'?callback='.$callback_address.'">點(diǎn)此登錄</a>'; 

?>
<iframe src="<?php echo $sso_address ?>?callback=<?php echo $callback_address ?>" frameborder="0"  width="0" height="0"></iframe>

login.php SSO登錄頁(yè)面:
復(fù)制代碼 代碼如下:
<?php 
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(emptyempty($_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 "系統(tǒng)檢測(cè)到您已登錄 {$_COOKIE['sign']} <a href="{$_GET['callback']}?{$query}">授權(quán)</a> <a href="?logout">退出</a>"; 

?>

callback.php 回調(diào)頁(yè)面用來(lái)設(shè)置跨域COOKIE:
復(fù)制代碼 代碼如下:
<?php 
header('Content-Type:text/html; charset=utf-8'); 
if(emptyempty($_GET)){ 
    exit('您還未登錄'); 
}else{ 
    foreach($_GET as $key=>$val){ 
        setcookie($key,$val,0,''); 
    } 
    header("location:index.php"); 
}
?>

connect.php 用來(lái)檢測(cè)登錄狀態(tài)的頁(yè)面,內(nèi)嵌在頁(yè)面的iframe中:
復(fù)制代碼 代碼如下:
<?php
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>

希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論