PHP implementation of Single sign-on instances

Source: Internet
Author: User
Tags decrypt script tag

1. Prepare two virtual domain names

127.0.0.1 www.a.com
127.0.0.1 www.b.com

2. Create the following files in the root directory of a

1 //index.php2 3<?PHP4 Session_Start();5?>6<! DOCTYPE html>789<meta charset= "UTF-8"/>Ten<title>sync login</title> One A<body> -  -<?phpif(Empty($_session[' username ']):?> the<p>hello, tourist; please <a href= "login.php" > Login </a></p> -<p><a href= "http://www.b.com/index.php" > enter space </a></p> -<?phpElse:?> -<p>hello,<?phpEcho $_session[' username '];? >;<a href= "http://www.b.com/index.php" > enter space </a></p> +<?phpendif;?> -<a href= "http://www.a.com/index.php" >home</a> +</body> A at  - //login.php -<?PHP - Session_Start(); - if(!Empty($_post[' username '])){ -     require'./des.php '; in     $_session[' username '] =$_post[' username ']; -     $redirect= ' http://www.a.com/index.php '; to     Header(' location:http://www.a.com/sync.php?redirect= '.UrlEncode($redirect).‘ &code= '. Des::encode ($_post[' username '], ' a ')); +     Exit; - } the?> *<! DOCTYPE html> $Panax Notoginseng -<meta charset= "UTF-8"/> the<title>sync login</title> + A<body> the<form action= "" method= "POST" > +<input type= "text" name= "username" placeholder= "user name"/> -<input type= "text" name= "password" placeholder= "password"/> $<input type= "Submit" value= "Login"/> $</form> -</body> - the  - //sync.phpWuyi<?PHP the $redirect=Empty($_get[' redirect ']) ? ' Www.a.com ':$_get[' redirect ']; - if(Empty($_get[' Code '])) { Wu     Header(' loaction:http://'.UrlDecode($redirect)); -     Exit; About } $  - $apps=Array( -' Www.b.com/slogin.php ' - ); A?> +<! DOCTYPE html> the - $<meta charset= "UTF-8"/> the<?phpforeach($apps  as $v):?> the<script type= "Text/javascript" src= "http://<?php Echo$v. '? code= '.$_get[' Code ']?> ></script> the<?phpEndforeach;?> the<title>passport</title> - in<body> the<script type= "Text/javascript" > theWindow.onload =function () { AboutLocation.replace (' <?php echo $redirect;?> '); the     } the</script> the</body> + -  the //des.phpBayi //When the session information is uploaded to a file under another domain name after www.a.com login, it is run in the form of a script tag.  the<?PHP the  - classDes - { the     /** the * Encryption of simple symmetric encryption algorithm the * @param string $string strings that need to be encrypted the * @param String $skey encryption Eky - * @return String the      */ the      Public Static functionEncode$string= ",$skey= ' cxphp ') the     {94         $STRARR=Str_split(Base64_encode($string)); the         $strCount=Count($STRARR); the         foreach(Str_split($skey) as $key=$value) { the             $key<$strCount&&$STRARR[$key] .=$value;98         } About         return Str_replace(Array(' = ', ' + ', '/'),Array(' o0o0o ', ' o000o ', ' oo00o '),Join(‘‘,$STRARR)); -     }101 102     /**103 * Decryption of simple symmetric encryption algorithm104 * @param string $string A string to decrypt the * @param String $skey Decrypt key106 * @return String107      */108      Public Static functionDecode$string= ",$skey= ' cxphp ')109     { the         $STRARR=Str_split(Str_replace(Array(' o0o0o ', ' o000o ', ' oo00o '),Array(' = ', ' + ', '/'),$string), 2);111         $strCount=Count($STRARR); the         foreach(Str_split($skey) as $key=$value) {113             $key<=$strCount&&isset($STRARR[$key]) &&$STRARR[$key][1] = = =$value&&$STRARR[$key] =$STRARR[$key][0]; the         } the         return Base64_decode(Join(‘‘,$STRARR)); the     }117}

3. Create the following file in the root directory of the www.b.com

1 //slogin.php File completion session settings2<?PHP3 Session_Start();4 Header(' Content-type:text/javascript; Charset=utf-8 ');5 if(!Empty($_get[' Code '])){6     require'./des.php ';7     $username= Des::d ecode ($_get[' Code '], ' a ');8     if(!Empty($username)){9         Header(' p3p:cp= ' CURa ADMa DEVa Psao psdo our BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR "');Ten         $_session[' username '] =$username; One     } A } -?> -  the //index.php -<?PHP - Session_Start(); - if(!Empty($_session[' username '])) + { -     Echo"Welcome."$_session[' username ']. " of space "; +}Else{ A     Echo"Please log in first"; at } -?>

4. Access www.a.com and www.b.com are not logged in at this time
Log in after two domain names

To this we have implemented a simple single sign-on.

done!

PHP implementation of Single sign-on instances

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.