Simple analysis of ucenter member synchronous login communication principle

  • 2021-07-16 02:05:28
  • OfStack

1. The user logs in to discuz and verifies the data from post through the function uc_user_login in logging. php file, that is, verifies username and password.

2. If the verification is successful, the function uc_user_synlogin in the file client. php under uc_client will be called, in which uc_api_post ('user', 'synlogin', array ('uid' = > $uid).

3. Then this function passes data to index. php of Ucenter, and index. php accepts the passed data, obtaining the values that model is user and action is synlogin.

4. Then index. php of Ucenter calls onsynlogin method in user. php class under control directory, and notifies uc application list to start synchronous login in javascript mode through foreach loop; That is to say, some data are transmitted to uc. php1 under api in each application directory through get.

5. uc. php receives the notification and processes the data from get, encrypts the data through function _ authcode in function synlogin (located in uc. php) (UC_KEY is used as the key by default), and sets cookie with function _ setcookie.

6. Each application decodes the cookie set above with a corresponding key to obtain user id and other data; This value is used to judge whether the user has logged in through other applications, so that the user can log in automatically.

logging. php for the application-- > client. php in uc_client---- > Ucenter------ > api/uc. php in other applications.

In fact, the principle of synchronous login of Ucenter is cookie. After an application logs in successfully, it transmits data to Ucenter, so that Ucenter notifies other applications to set cookie, so that users can automatically log in through the set cookie when accessing other applications.

First, install ucenter and then copy the folder uc_client to your own project, then configure several files

client. php is equivalent to a function library

uc. php is equivalent to a callback file

config. php is the configuration file

When you have two applications that are set to log in synchronously, when you log in to one application and then execute it

include './config.inc.php';
include './uc_client/client.php';
$usernames="kyee";
$passwords="123456";
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 Successful';
} 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 synchronous login to all other functions that open synchronous login. uc itself will loop through all applications that open synchronous login in the background once and then output them on the page

< script type="text/javascript" src="http://rayibeauty.ck101.com/api/uc.php?time=1408327309 & amp;code=bc6bFLa6WH343nin2GAn%2F82Y9cnCennPk1gcLGYHdQF4wsXsOSdTyqBb2Nuoxe0UJqzWMWncdx%2FfQ1GK6FS%2BqJqi2AxVG2Oq1pD9c1wZy%2BgjXs7qo4mm2sxFVHwW7JnjKGPDkVdDqtYeybkSISz7yrdb0ZFuXH2yr3Cq" reload="1" > < /script >

< script type="text/javascript" src="http://kibeauty.ibeauty.tw/api/uc.php?time=1408327309 & amp;code=206flCqeb%2Faft%2FDFPno9Bvqsb1b0o6XTZdIByOoD7EC11vMrjzC7PaKLo0LF3tGiHwlwZkwdW5VDHq866MGulsco5nekfkL341VWp7BPabnZPNtgG7m4jZpfdx6nVP0LTJLYI%2BkebI7uLm58atk8Ex4sKBj%2FfDkjH%2F8z" reload="1" > < /script >

js code like this, It is sent to each application that opens synchronous login, and then the callback file uc. php of each application that opens synchronous login will be decrypted after receiving it. After decryption, you can actually write the code by yourself. The code of this uc. php callback file must not be written in their format, or you can write your own code by yourself.

In fact, the principle of UC is very simple, that is, after an application logs in, it polls the callback file sent to the synchronized login application in the background. After the callback file receives the user ID, it generates cookie or session and then enters the login mode.


Related articles: