php的sso单点登录实现方法
程序员文章站
2022-12-22 18:51:10
本文实例讲述了php的sso单点登录实现方法。分享给大家供大家参考。具体分析如下:
这里详细讲到了几点:
1、点击登录跳转到sso登录页面并带上当前应用的callbac...
本文实例讲述了php的sso单点登录实现方法。分享给大家供大家参考。具体分析如下:
这里详细讲到了几点:
1、点击登录跳转到sso登录页面并带上当前应用的callback地址
2、登录成功后生成cookie并将cookie传给callback地址
3、callback地址接收sso的cookie并设置在当前域下再跳回到应用1即完成登录
4、再在应用程序需要登录的地方嵌入一个iframe用来实时检测登录状态,代码如下:
index.php 应用程序页面:
复制代码 代码如下:
<?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地址用于回调设置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>
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地址用于回调设置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>
login.php sso登录页面:
复制代码 代码如下:
<?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 "系统检测到您已登录 {$_cookie['sign']} <a href="{$_get['callback']}?{$query}">授权</a> <a href="?logout">退出</a>";
}
?>
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 "系统检测到您已登录 {$_cookie['sign']} <a href="{$_get['callback']}?{$query}">授权</a> <a href="?logout">退出</a>";
}
?>
callback.php 回调页面用来设置跨域cookie:
复制代码 代码如下:
<?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");
}
?>
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 用来检测登录状态的页面,内嵌在页面的iframe中:
复制代码 代码如下:
<?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>
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>
希望本文所述对大家的php程序设计有所帮助。
上一篇: 在Python中关于使用os模块遍历目录的实现方法
下一篇: Python多图片合并PDF的方法