Single sign-on Ucenter Sample Analysis _php instance

Source: Internet
Author: User
Tags php class setcookie
First let's take a look at the next Ucenter login step





1, the user login discuz, through the logging.php file function Uc_user_login to post the data validation, that is, username and password to verify.


2. If the validation succeeds, the function Uc_user_synlogin located in the client.php file under Uc_client will be invoked to invoke Uc_api_post (' user ', ' synlogin ', Array (' UID ') in this function. > $uid)).


3, then this function to Ucenter index.php pass the data, index.php accept the transmitted data, get model user,action for the Synlogin value.


4, Then Ucenter's index.php invokes the Onsynlogin method in the User.php class under the control directory, using the Foreach loop to notify the UC application list of the application to open the synchronous login in a synchronous logon; that is, passed to the uc.php some data with the API in the directory.


5, uc.php receives the notification and processing get over the data, and in the function Synlogin (in uc.php) through the function _authcode encrypts the data (by default Uc_key as the key), uses the function _setcookie to set the cookie.


6, each application with the corresponding key to decode the cookie set above, get the user ID and other data, through this value to determine whether the user has been logged through other applications, so that users can automatically login.





Application logging.php------client.php in >uc_client------>ucenter------> Applications api/uc.php





In fact, ucenter implementation of the principle of synchronous landing is a cookie, a successful application after landing, to Ucenter pass data, let Ucenter notify other applications also set cookies, so that users access to other applications through the set of cookies to achieve automatic landing. Understanding the principle of Ucenter synchronization, and then encountered can not sync landing, or to develop some and ucenter interface when it will be much easier.





General steps first we need to install Ucenter and then copy the Uc_client folder into our project. Configure several files





client.php equivalent function library





uc.php equivalent to callback file





There is also a config.inc.php is a configuration file





When you have 2 applications that are set up synchronously after landing when you log in an application then execute


Copy Code code as follows:

Include './config.inc.php ';
Include './uc_client/client.php ';
$usernames = "Feiye";
$passwords = "789123";
List ($uid, $username, $password, $email) = Uc_user_login ($usernames, $passwords);
if ($uid > 0) {
Setcookie ("username", $username, Time () +intval (24*3600));
echo Uc_user_synlogin ($UID);
Echo ' Login succeeded ';
} elseif ($uid = = 1) {
Echo ' user does not exist, or is deleted ';
} elseif ($uid = = 2) {
echo ' wrong password ';
} else {
echo ' undefined ';
}



Uc_user_synlogin () This function represents the simultaneous login to all other open sync login functions UC itself will be in the background to all open synchronous landing applications to loop through and then on the page output


Copy Code code as follows:

<script type= "Text/javascript" src= "http://127.0.0.70/api/uc.php?time=1374540644&code=14fdIufn% 2b2ywkqln9p07fehofzvdjupvgbgarpn7r0djmbewcb23vkwo1uaeyblq3hzhtokozrnqu7ngi09jzs684drfcbldispkhk6p50mftbra3vp4yiswhrpml1dx O5AJB7CVZ9F8EI%2BKDFFQ0E0RDYERKUZ8GOEHHG "reload=" 1 "></script>



Copy Code code as follows:

<script type= "Text/javascript" src= "http://127.0.0.71/api/uc.php?time=1374540644&code= 07a91g8sepqwwfa3c1un1sphc4v6yuer1jfbvtqmk%2bqzmtkjwoz8x% 2b8rwgnmkdhlxe9xxvqqakdjn26ck6bma19zploisw4wunnxhab9xxlt2vexuyf03mnehac% 2BUDJWB58SBXGCYUPM4BMZDM3Q92OBSP0KK2QCD12FQG "reload=" 1 "></script>



This kind of JS code is sent to each open synchronous landing applications and then each open synchronous login application of the callback file uc.php received will be decrypted and decrypted well, you can actually write the code yourself. The code for this uc.php callback file does not have to be written in their format. You can also Write your own code for example, I just follow the session to do the synchronous landing


Copy Code code as follows:

function Synlogin ($get, $post) {
$uid = $get [' uid '];
$username = $get [' username '];
if (! Api_synlogin) {
return Api_return_forbidden;
}
Header (' p3p:cp= "cura ADMa DEVa Psao psdo We bus UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"];
Setcookie (' Gwyy ', $username, Time () +3600, '/', ' 127.0.0.71 ');
_setcookie (' Example_auth ', _authcode ($uid.) \ t ". $username, ' ENCODE '));

$_session[' username ' = $username;
$_session[' uid ' = $uid;
}

function Synlogout ($get, $post) {
if (! Api_synlogout) {
return Api_return_forbidden;
}
Note Sync log out API interface
Header (' p3p:cp= "cura ADMa DEVa Psao psdo We bus UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"];
_setcookie (' Example_auth ', ',-86400 * 365);
unset ($_session[' username '));
unset ($_session[' uid '));
Session_destroy ();
}



This will automatically log in when the user refreshes another application page.





Note that if you add an application to UC and the communication does not succeed, then the reason is very simple is not found you http://xxxx/api/uc.php this file as long as there is this file, the communication will be successful





In fact, the principle of UC is very simple is an application after landing and then the background polling sent to the synchronous landing of the application callback file callback file received after the user ID generated cookies or session and then enter the login mode.

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.