Use PHP to implement a single sign-on instance.

Source: Internet
Author: User

1. Prepare two virtual domain names first

127.0.0.1  www.openpoor.com127.0.0.1  www.myspace.com

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

index.php file

<?PHPSession_Start(); ?> <! DOCTYPE html> if(Empty($_session[' username ']):?>Hello, tourists; please <a href= "login.php" > Login </a><a href= "http://www.myspace.com/index.php" > enter space </a> < PhpElse:?>Hello, <?phpEcho $_session[' username '];? >;<a href= "http://www.myspace.com/index.php" > enter space </a> <?phpendif;?> <a href= "http://www.openpoor.com/index.php" >home</a> </body> 

login.php file

<?PHPSession_Start(); if(!Empty($_post[' username '])){    require‘.. /des.php '; $_session[' username '] =$_post[' username ']; $redirect= ' http://www.openpoor.com/index.php '; Header(' location:http://www.openpoor.com/sync.php?redirect= '.UrlEncode($redirect).‘ &code= '. Des::encrypt ($_post[' username '], ' openpoor ');Exit; }  ?> <! DOCTYPE html> 

sync.php file

<?PHP$redirect=Empty($_get[' redirect ']) ? ' Www.openpoor.com ':$_get[' redirect ']; if(Empty($_get[' Code '])){      Header(' loaction:http://'.UrlDecode($redirect)); Exit; }  $apps=Array(    ' Www.myspace.com/slogin.php '  ); ?> <! DOCTYPE html> foreach($apps  as $v):?> <script type= "Text/javascript" src= "http://<?php Echo$v.‘? Code= '.$_get[' Code ']?> "></script> <?phpEndforeach;?> <title>passport</title> window. onload=function() { location. replace (' <?php echo $redirect;?> '); }  </script> </body> 

3. Create the following files in the root directory of MySpace

Slogin File Completion Session settings

<?PHPSession_Start(); Header(' Content-type:text/javascript; Charset=utf-8 '); if(!Empty($_get[' Code '])){    require‘.. /des.php '; $username= Des::d ecrypt ($_get[' Code '], ' Openpoor '); if(!Empty($username)){      Header(' p3p:cp= ' CURa ADMa DEVa Psao psdo our BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR "'); $_session[' username '] =$username; }  }  ?>

index.php

<? PHP   Session_Start ();   if (! Empty ($_session[' username ']))  {      echo "Welcome". $_session [' username ']. " The space ";  } Else {      echo "Please login First";  }  ? >  

4.des.php file content is as follows

<?PHP/** * @see Yii Csecuritymanager; */  classdes{ Public Static functionEncrypt$data,$key){        $module=mcrypt_module_open (' des ', ', MCRYPT_MODE_CBC, '); $key=substr(MD5($key), 0,mcrypt_enc_get_key_size ($module)); Srand(); $iv=mcrypt_create_iv (Mcrypt_enc_get_iv_size ($module),Mcrypt_rand); Mcrypt_generic_init ($module,$key,$iv); $encrypted=$iv. Mcrypt_generic ($module,$data); Mcrypt_generic_deinit ($module); Mcrypt_module_close ($module); return MD5($data).‘ _‘.Base64_encode($encrypted); }     Public Static functionDecrypt$data,$key){            $_data=Explode(‘_‘,$data, 2); if(Count($_data) <2){      return false; }        $data=Base64_decode($_data[1]); $module=mcrypt_module_open (' des ', ', MCRYPT_MODE_CBC, '); $key=substr(MD5($key), 0,mcrypt_enc_get_key_size ($module)); $ivSize=mcrypt_enc_get_iv_size ($module); $iv=substr($data, 0,$ivSize); Mcrypt_generic_init ($module,$key,$iv); $decrypted=mdecrypt_generic ($module,substr($data,$ivSize,strlen($data))); Mcrypt_generic_deinit ($module); Mcrypt_module_close ($module); $decrypted=RTrim($decrypted, "the"); if($_data[0]!=MD5($decrypted)){      return false; }        return $decrypted; }  }  ?>

When the Openpoor log in after the session information to other domain names under the file for processing, in the form of script tags to run.


5. Access www.openpoor.com and www.myspace.com are not logged in at this time

Log in after two domain names

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

Https://mp.weixin.qq.com/s/4h_SA1TUR7hJWO1MSS2jtg

Use PHP to implement a single sign-on instance.

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.