Home > php教程 > php手册 > body text

php的sso单点登录实现方法,phpsso单点登录

WBOY
Release: 2016-06-13 09:17:23
Original
1085 people have browsed it

php的sso单点登录实现方法,phpsso单点登录

本文实例讲述了php的sso单点登录实现方法。分享给大家供大家参考。具体分析如下:

这里详细讲到了几点:
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://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']} 退出"); 
}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(emptyempty($_COOKIE['sign'])){ 
?>
 

 

用户名:

 

密  码:

 
 
 

 
 
}else{ 
    $query = http_build_query($_COOKIE); 
    echo "系统检测到您已登录 {$_COOKIE['sign']} 授权 退出"; 

?>
callback.php 回调页面用来设置跨域COOKIE:

复制代码 代码如下:

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中:

复制代码 代码如下:

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; 

?>

希望本文所述对大家的php程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!