Home >Backend Development >PHP Tutorial >Analysis of php+mysql large user login solution, mysql user login_PHP tutorial
This article analyzes the php+mysql mass user login solution through examples. Share it with everyone for your reference. The specific analysis is as follows:
Big companies such as Baidu, QQ, 360, etc. all have hundreds of millions of users. Not only do all sub-websites log in through one account, but they also open user platforms for other websites to use. This level of data volume and visits , if optimization is not done, it will probably go down soon. These companies have a dedicated team to maintain a registration and login, and the detailed design is very excellent. Now let’s briefly talk about their design plans.
When it comes to big data, the pressure is not on PHP, but mainly on MySQL. PHP can do load balancing. If 10 machines cannot handle it, just use 20 or 100. This is not a bottleneck.
But MySQL is a single point. No matter how many slaves are made, the query is optimized. Updating the data cannot be solved simply by adding a machine. Moreover, the query can also reduce the pressure through Memcache caching, so there is no need to do many slaves. For libraries, usually 1 master and 4 slaves are enough.
The following mainly introduces the database solutions:
Assume that users can log in through "login name", "email" or "mobile phone number".
The table structure is as follows:
Login name and ID table, divided into 100 tables according to login_hash
CREATE TABLE user_info(
user_id BIGINT user ID
login_pwd CHAR() User login password
… …Other information, home address, mobile phone number, gender, etc.
);
CREATE TABLE user_info0 LIKE user_info;
CREATE TABLE user_info1 LIKE user_info;
… …
CREATE TABLE user_info2 LIKE user_info;
Depend on the server: implement a self-increasing ID service (equivalent to Oracle's sequence), or you can implement it yourself (either using PHP+MySQL or C). The purpose is to get the ID from this service. The number of IDs taken each time is +1 based on the last time. It is very similar to MySQL's autoincrement, except that it cannot be incremented inside the table.
Registration process:
1) Verify user name, email, mobile phone number, password and other formats. Omit…
2) Get an ID from the service, assuming it is 115.
3) If the user's login type is email (eg: $loginName='songhuan@zixue.it'), add the prefix login name result before the login name (eg: $loginName='mail_songhuan@zixue.it' )
4) Find the HASH value of the login name: $loginHash=md5($loginName); For the hash of the md5 value, you can find the asc code, or use your own algorithm, and finally get $loginHash=16-bit or 32-bit integer
5) $tableName = 'user_login' . ($loginHash%100), if you get the user_login table name, if the result is user_login88.
$tableName = 'user_info' . (115%100), if you get the user_info table name.
6) Execute SQL:
1) If the user's login type is email (eg: $loginName='songhuan@zixue.it'), add the prefix login name result before the login name (eg: $loginName='mail_songhuan@zixue.it' )
2)
3)
4) Execute SQL:
If the data cannot be queried, the login name does not exist
5) If id=115 can be obtained, then
SELECT id, pwd … FROM user_info15 WHERE id = 115;
6) Match passwords, if the passwords are not equal, return false
7) If the passwords are equal, encrypt the user ID and put it in COOKIE, and store the user information in Memcache.
I hope this article will be helpful to everyone’s PHP programming design.