Php sso Single Sign-On Implementation Method

Source: Internet
Author: User
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;
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.