php sso single sign-on implementation method, phpsso single sign-on
The example in this article describes the implementation method of PHP's SSO single sign-on. Share it with everyone for your reference. The specific analysis is as follows:
Here are some details:
1. Click Login to jump to the SSO login page and bring the callback address of the current application
2. After successful login, COOKIE is generated and passed to the callback address
3. The callback address receives the SSO COOKIE and sets it in the current domain, then jumps back to application 1 to complete the login
4. Then embed an iframe in the place where the application needs to log in to detect the login status in real time. The code is as follows:
index.php application page:
Copy code The code is as follows:
header('Content-Type:text/html; charset=utf-8');
$sso_address = 'http://XXXX.com/sso/login.php'; //The domain name of your SSO
$callback_address = 'http://'.$_SERVER['HTTP_HOST']
.str_replace('index.php','',$_SERVER['SCRIPT_NAME'])
.'Callback.php '; // callback address is used to set up cookie
if(isset($_COOKIE['sign'])){
exit("Welcome{$_COOKIE['sign']}
Exit");
}else{
echo 'You are not logged in yet
Click here to log in';
}
?>
login.php SSO login page:
Copy code The code is as follows:
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 "The system has detected that you have logged in to {$_COOKIE['sign']}
Authorization Logout";
}
?>
callback.php callback page is used to set cross-domain COOKIE:
Copy code The code is as follows:
header('Content-Type:text/html; charset=utf-8');
if(emptyempty($_GET)){
exit('You are not logged in yet');
}else{
foreach($_GET as $key=>$val){
setcookie($key,$val,0,'');
}
header("location:index.php");
}
?>
connect.php is a page used to detect login status, embedded in the iframe of the page:
Copy code The code is as follows:
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;
}
?>
I hope this article will be helpful to everyone’s PHP programming design.
http://www.bkjia.com/PHPjc/939404.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/939404.htmlTechArticlephp sso single sign-in implementation method, phpsso single sign-in This article describes the php sso single sign-in implementation as an example method. Share it with everyone for your reference. The specific analysis is as follows: Let’s talk in detail here...