The example in this article describes the SSO single Sign-on implementation method for PHP. Share to everyone for your reference. The specific analysis is as follows:
Here are a few details:
1, click the login to jump to the SSO login page and with the current application of the callback address
2, after the successful login to generate cookies and pass the cookie to the callback address
3, callback address to receive the SSO cookie and set up under the current domain and then jump back to apply 1 to complete the login
4, again in the application needs to login to embed an IFRAME for real-time detection of login status, the code is as follows:
index.php Application Page:
Copy Code code as follows:
<?php
Header (' content-type:text/html; Charset=utf-8 ');
$sso _address = ' http://XXXX.com/sso/login.php '; The domain name where you have SSO
$callback _address = ' http://'. $_server[' Http_host ']
. Str_replace (' index.php ', ', ', $_server[' script_name ')]
.' Callback.php '; Callback address for callback settings cookie
if (isset ($_cookie[' sign ')) {
Exit ("Welcome to {$_cookie[' sign ']} <a href=" Login.php?logout "> Exit </a>");
}else{
Echo ' You have not logged in <a href= '. $sso _address. ' Callback= '. $callback _address. ' " > Point this login </a> ';
}
?>
<iframe src= "<?php echo $sso _address >?callback=<?php echo $callback _address?>" frameborder= "0" width= "0" height= "0" ></iframe>
login.php SSO Login Page:
Copy Code code as follows:
<?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> User name: <input type= "text" name= "username"/></p>
<p> Password: <input type= "password" name= "password"/></p>
<input type= "hidden" name= "callback" value= "<?php Echo $_get[' callback '";?> "/>"
<input type= "Submit" value= "Login"/>
</form>
<?php
}else{
$query = Http_build_query ($_cookie);
echo "System detects that you have logged on to {$_cookie[' sign ']} <a href= ' {$_get[' callback ']}? {$query} "> Authorization </a> <a href="? Logout "> Exit </a>";
}
?>
The callback.php callback page is used to set Cross-domain cookies:
Copy Code code as follows:
<?php
Header (' content-type:text/html; Charset=utf-8 ');
if (Emptyempty ($_get)) {
Exit (' You are not logged in ');
}else{
foreach ($_get as $key => $val) {
Setcookie ($key, $val, 0, ");
}
Header ("location:index.php");
}
?>
Connect.php is used to detect the login status of the page, embedded in the page of the IFRAME:
Copy Code code as follows:
<?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
}
?>
I hope this article will help you with your PHP program design.