Home  >  Article  >  php教程  >  Simple analysis of ucenter member synchronous login communication principle

Simple analysis of ucenter member synchronous login communication principle

高洛峰
高洛峰Original
2016-12-30 16:44:341227browse

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

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

3. Then this function passes data to index.php of Ucenter. Index.php accepts the passed data and obtains the value of model as user and action as synlogin.

4. Then Ucenter’s index.php calls the onsynlogin method in the user.php class in the control directory, and uses javascript to notify the applications that enable synchronous login in the uc application list to log in synchronously through the foreach loop; That is, some data is passed to uc.php under the api in each application directory through get method.

5. uc.php receives the notification and processes the data obtained, and encrypts the data through the function _authcode in the function synlogin (located in uc.php) (default uses UC_KEY as the key), and uses the function _setcookie Set cookies.

6. Each application uses the corresponding key to decode the cookie set above to obtain the user ID and other data; use this value to determine whether the user has logged in through other applications, so that the user can log in automatically.

Logging.php in application ------>client.php in uc_client------>Ucenter------>api/ in other applications uc.php.

In fact, the principle of Ucenter to achieve synchronous login is cookie. After an application successfully logs in, it transfers data to Ucenter and lets Ucenter notify other applications to also set cookies, so that users can use the already set cookies when accessing other applications. Cookie enables automatic login.

The general steps are to first install ucenter and then copy the uc_client folder to your own project, and then configure several files

client.php is equivalent to the function library

uc.php is equivalent to the callback file

config.php is the configuration file

When you have two applications that have set up synchronous login, when you log in to an application and execute

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 '登录成功';
} elseif($uid == -1) {
echo '用户不存在,或者被删除';
} elseif($uid == -2) {
echo '密码错误';
} else {
echo '未定义';
}

uc_user_synlogin() This function represents the synchronous login to all other functions that enable synchronous login. uc itself will loop through all the applications that enable synchronous login in the background and then output it on the page

<script type="text/javascript" src="http://rayibeauty.ck101.com/api/uc.php?time=1408327309&code=bc6bFLa6WH343nin2GAn%2F82Y9cnCennPk1gcLGYHdQF4wsXsOSdTyqBb2Nuoxe0UJqzWMWncdx%2FfQ1GK6FS%2BqJqi2AxVG2Oq1pD9c1wZy%2BgjXs7qo4mm2sxFVHwW7JnjKGPDkVdDqtYeybkSISz7yrdb0ZFuXH2yr3Cq" reload="1"></script>
<script type="text/javascript" src="http://kibeauty.ibeauty.tw/api/uc.php?time=1408327309&code=206flCqeb%2Faft%2FDFPno9Bvqsb1b0o6XTZdIByOoD7EC11vMrjzC7PaKLo0LF3tGiHwlwZkwdW5VDHq866MGulsco5nekfkL341VWp7BPabnZPNtgG7m4jZpfdx6nVP0LTJLYI%2BkebI7uLm58atk8Ex4sKBj%2FfDkjH%2F8z" reload="1"></script>

JS code like this is sent to each application that enables synchronous login, and then the callback file uc.php of each application that enables synchronous login will be decrypted after receiving it. After decryption, you can actually write it yourself. The code of this uc.php callback file does not have to be written in their format. You can also write your own code.

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

For more simple analysis of ucenter member synchronous login communication principles and related articles, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn