This article mainly introduces the implementation of sso Single Sign-On in php. The example analyzes the principles and implementation steps of sso Single Sign-On, which has some reference value. For more information, see
This article mainly introduces the implementation of sso Single Sign-On in php. The example analyzes the principles and implementation steps of sso Single Sign-On, which has some reference value. For more information, see
This article describes how to implement sso in php. Share it with you for your reference. The specific analysis is as follows:
The following details:
1. Click login to jump to the SSO login page and bring the callback address of the current application
2. Generate a COOKIE upon successful login and send the COOKIE to the callback address
3. The callback address receives the sso cookie and sets it to jump back to Application 1 under the current domain to complete the login.
4. embed an iframe where the application needs to log on to the system to detect the logon status in real time. The Code is as follows:
Index. php application page:
The Code is as follows:
<? Php
Header ('content-Type: text/html; charset = UTF-8 ');
$ Sso_address = 'HTTP: // xxxx.com/sso/login.php'; // your SSO Domain Name
$ Callback_address = 'HTTP: // '. $ _ SERVER ['HTTP _ host']
. Str_replace ('index. php', '', $ _ SERVER ['script _ name'])
. 'Callback. php'; // The callback address is used to set the cookie for callback.
If (isset ($ _ COOKIE ['sign']) {
Exit ("Welcome to exit {$ _ COOKIE ['sign ");
} Else {
Echo 'you have not logged on. Click here to log on ';
}
?>
Login. php SSO logon page:
The Code is 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']) {
?>
<? Php
} Else {
$ Query = http_build_query ($ _ COOKIE );
Echo "the system has detected that you have logged on to {$ _ COOKIE ['sign']} and authorized to exit ";
}
?>
The callback. php callback page is used to set cross-origin cookies:
The Code is as follows:
<? Php
Header ('content-Type: text/html; charset = UTF-8 ');
If (emptyempty ($ _ GET )){
Exit ('you have not logged on to it ');
} Else {
Foreach ($ _ GET as $ key => $ val ){
Setcookie ($ key, $ val, 0 ,'');
}
Header ("location: index. php ");
}
?>
Connect. php is used to check the login status page, embedded in the iframe of the page:
The Code is 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;
}
?>