php sso單點登入實現代碼_PHP教程

來源:互聯網
上載者:User
下面詳細講到了幾點。1、點擊登入跳轉到SSO登入頁面並帶上當前應用的callback地址2、登入成功後產生COOKIE並將COOKIE傳給callback地址3、callback地址接收SSO的COOKIE並設定在當前域下再跳回到應用1即完成登入4、再在應用程式需要登入的地方嵌入一個iframe用來即時檢測登入狀態
代碼如下 複製代碼


//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']} 退出");
}else{
echo '您還未登入 點此登入';
}
?>

//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'])){
?>


}else{
$query = http_build_query($_COOKIE);
echo "系統檢測到您已登入 {$_COOKIE['sign']} 授權 退出";
}
?>

//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");
}
?>

//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;
}
?>


http://www.bkjia.com/PHPjc/444725.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/444725.htmlTechArticle下面詳細講到了幾點。1、點擊登入跳轉到SSO登入頁面並帶上當前應用的callback地址2、登入成功後產生COOKIE並將COOKIE傳給callback地址3、callba...

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.