Php sso Single Sign-on implementation code

Source: Internet
Author: User

The following describes in detail. 1. Click Log On To Go To The SSO logon page and bring the callback address of the current application 2. Generate a COOKIE after successful logon and send the COOKIE to the callback address 3. The callback address receives the sso cookie and is set to the current the domain jumps back to Application 1 to complete logon. 4. Then, an iframe is embedded in the place where the application needs to log on to the domain to detect the logon status in real time.

The Code is as follows: Copy code

 


<? Php
// Index. php application page
Header ('content-Type: text/html; charset = UTF-8 ');
$ Sso_address = 'HTTP: // 2spaoku.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 {$ _ COOKIE ['sign']} <a href =" login. php? Logout "> exit </a> ");
} Else {
Echo 'Have you logged on to <a href = "'. $ sso_address .'? Callback = '. $ callback_address.' "> click here to log on </a> ';
}
?>
<Iframe src = "<? Php echo $ sso_address?>? Callback = <? Php echo $ callback_address?> "Frameborder =" 0 "width =" 0 "height =" 0 "> </iframe>

<? Php
// Login. php SSO logon page
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 (empty ($ _ 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 "the system has detected that you have logged on to {$ _ COOKIE ['sign']} <a href =" {$ _ GET ['callback']}? {$ Query} "> authorization </a> <a href = "? Logout "> exit </a> ";
}
?>

<? Php
// The callback. php callback page is used to set cross-domain cookies.
Header ('content-Type: text/html; charset = UTF-8 ');
If (empty ($ _ GET )){
Exit ('you have not logged on to it ');
} Else {
Foreach ($ _ GET as $ key => $ val ){
Setcookie ($ key, $ val, 0 ,'');
}
Header ("location: index. php ");
}
?>

<? Php
// Connect. php is used to check the login status page, embedded in the iframe of the page
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;
}
?>
<Html> <script type = "text/javascript"> top. location. href = "<? Php echo $ callback;?> "; </Script>

 


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.