First, let’s learn about the Ucenter login steps
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)) will be called.
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, through get method to pass some data to uc.php under the api in each application directory.
5. uc.php receives the notification and processes the data obtained, encrypts the data through function _authcode in function synlogin (located in uc.php) (default uses UC_KEY as the key), and sets COOKIE with function _setCOOKIE.
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 the application ——>client.php in uc_client——>Ucenter——>api/uc.php in the application
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 COOKIE, so that users can use the already set COOKIE when accessing other applications. Log in automatically. After understanding the synchronization principle of Ucenter, it will be much easier when you encounter problems with synchronous login or developing some interfaces with UCenter.
General steps: First we need to install ucenter and then copy the uc_client folder to our own project and then configure several files
client.php is equivalent to a function library
uc.php is equivalent to a callback file
There is also a config.inc.php which is a configuration file
When you have two applications set up to synchronize login, when you log in to an application Then execute
The code is 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 successful’;
} elseif( $uid == -1) {
echo ‘The user does not exist or has been deleted’;
} elseif($uid == -2) {
echo ‘Wrong password’;
} else {
echo ‘undefined’;
} uc_user_synlogin() This function represents the need to synchronize login to all other functions that enable synchronous login. uc itself will loop through all applications that enable synchronous login in the background and then Output
on the page. The code is as follows:
The code is as follows:
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 being received. After decryption, you can actually write the code 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. For example, I do synchronous login based on session
The code is 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 OUR 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 Synchronous logout API interface
header(‘P3P: CP=”CURa ADMa DEVa PSAo PSDo OUR 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() ;
} In this way, when the user refreshes other application pages, he will automatically log in
Note that if adding an application in UC prompts that the communication is not successful, the reason is simply that you are not found http://xxxx/ As long as this file api/uc.php exists, the communication will definitely be successful
In fact, the principle of UC is very simple. After an application logs in, it then polls the callback file callback sent to the synchronously logged-in application in the background. After the file receives the user ID, it generates a COOKIE or session and then enters the login mode.