This time for everyone to bring PHP Single sign-on function implementation, PHP Single sign-on function to realize the attention of what, the following is the actual case, together to see.
This article describes the PHP implementation of single sign-on functionality. Share to everyone for your reference, as follows:
1. Prepare two virtual domain names
127.0.0.1 www.openpoor.com
127.0.0.1 www.myspace.com
2. Create the following files in the root directory of the Openpoor
Index. Php
<?phpsession_start ();? ><! DOCTYPE html>
login.php
<?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
<?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>
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 C or "'); $_session[' username '] = $username; }}?>
index.php
<?phpsession_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; */class des{public static function encrypt ($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 function Decrypt ($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, "n"); 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.
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Recommended reading:
Implementation of PHP One-way hash encryption function
How to use the static variables of PHP functions