Most friends who are engaged in PHP development know that there is ucenter, which can be used to log in and log out of multiple sites at the same time, and synchronize users. Next, the author will share with you how ucenter implements synchronous login.
First of all, the site must introduce a client such as uc_client. Taking login as an example, an external function will be called first when logging in. uc_user_login under uc_client/client.php checks whether this user exists. If it is correct, continue to execute the login code. , detect this user process: according to how the user connects to uc (mysql or http post), and then use different functions to detect the user accordingly. After the detection is completed, local login is implemented, the code is written by oneself, and then other applications are also logged in. , at this time the external function uc_user_synlogin is called, the process is as follows:
Uc_user_synlogin calls uc_api_post to implement which module to use. For example, it is the user module now, and the parameter passed is user_id. I don’t look at the details of how this is implemented. In short, the data is sent to uc_api, which is your ucenter address. index.php, and also attaches some parameters. Based on these parameters, it reads all the communicable applications in it and allows them to log in. This is a matter for other programs.
Notifying other applications mainly uses ucenter to send some data to api/uc.php under each application. The data is received by uc.php and then performs corresponding login operations.
This is what I wrote. It’s basically the same as his. I wanted to write down every function in detail but I’m a little tired today. Haha, it’s easy to find it yourself.
1. Start with user xxx entering the user name and password in login.php of a certain application. First use the uc_user_login function to verify the user and password on the uc server. If correct, write the session, write cookies, and update the login IP and login time in the application membership table. The user cannot feel this process.
2. Then notify the uc server through uc_user_synlogin that user xxx has successfully logged in. This process may use ajax, and the user will not feel the notification process.
3. After receiving this message, the uc server immediately ordered its subordinates to send the xxx login message, like a token ring, to all other applications that are willing to receive this message (whether the synchronous login is enabled in the background). In fact, it is to access the uc.php of each application with parameters, and the user cannot feel this process.
4. Each application relies on uc.php under the API to receive messages from the uc server, obey the instructions of the uc server, and do whatever it asks. Now, after receiving the command for user xxx to log in to your program, execute it immediately. And write the session of this application, and use p3p to write cookies in the same domain or different domains. The user cannot feel this process.
5. Finally, all programs integrated with uc, xxx logged in successfully. After the user logs in from www.phpernote.com/bbs, the login is also displayed when jumping to www.phpernote.com/. Because both the bbs and news systems are logged in in the background.
6. The session between the application and the uc server ends.