Php user login code (Limit the number of user logon errors) (1/2)

Source: Internet
Author: User
Tags mysql tutorial

Php user login code (Limit the number of user logon errors)

Php user logon code (Limit the number of user logon errors)

Session_start ();
Include ("mysql tutorial. class. php ");
$ Db = new mysql ('localhost', 'root', '', 'flx', 'conn', 'gbk ');
// To facilitate the test, a specific user is simply defined here. In actual operations, the user information is obtained from the database tutorial;
Define ('user', 'admin ');
Define ('PW, 123 );
$ Db-> findall ('limit _ login ');
$ Row = $ db-> fetch_array ();
// Print_r ($ row );
If ($ _ post ['submit ']) {
$ Username = $ _ post ["username"];
$ Pw = $ _ post ["pw"];
$ _ Session ['time'] = time ();
/* Determine if the number of errors exceeds the limit */
// For users with logon errors more than three times, if the current time minus the start time, is less than the specified value, it is not allowed
Xu is logging on;
If ($ username = user ){
// Here, you can try to log on three times, and write it below as 2 because the page refresh is not synchronized with the database connection,
// That is to say, the information obtained here is the first time when the second error message is inserted into the database.
The number of times has been more than once;
If ($ row ['login _ times ']> 2 ){
$ Current = time ();
$ Passed_time = $ current-$ row ['login _ date'];
Echo '. $ passed_time.' Second <br> ';
// Set the time to 900 seconds. During this period, a specific user cannot log on to the system.
If ($ current-$ row ['login _ date']) <900 ){
// $ Test_pw = substr (md5 (rand (), 0, 10 );
// Now the admin (specific user) user has logged on more than 3 times, so within the limited time,
// You cannot log on to the system even if the password is entered correctly;
If ($ pw = pw ){
Exit ('you have entered more than three wrong password attempts. Please log on to the system in 15 minutes! ');
}
// Of course, this prompt is also given if the password entered by this user is not the correct one;
If ($ pw! = Pw ){
Exit ('you have entered more than three wrong password attempts. Please log on to the system in 15 minutes! ');
}
} Else {
// The time limit has been reached. This user can log on. Before logging on, the user must set the time of the previous login.
Cleared;
$ Mod_content = "'login _ times '= 0, 'login _ date' = 0 ";
$ Condition = "'id' = 1 ";
$ Db-> update ('limit _ login', $ mod_content, $ condition );
}
}
}

 

/* User Login operation */

If ($ username & $ pw ){
If ($ username = user & $ pw = pw ){
/* The user logs in successfully ,*/
// Update the data table. Set the table logon times and time to zero;
$ Mod_content = "'login _ times '= 0, 'login _ date' = 0 ";
$ Condition = "'id' = 1 ";
$ Db-> update ('limit _ login', $ mod_content, $ condition );
Echo '<script language = "webpage effect" type = "text/Webpage effect"> '.
'Window. location = "success. php" </script> ';
} Else {
// When an error occurs for the first time, set the number of password errors to 1 and put the session saving time during login into the database;
If ($ row ['login _ times '] = 0 & $ row ['login _ date'] = 0 ){
$ Mod_content = "'login _ times '= 1, 'login _ date' ='". $ _ session ['time']. "'";
$ Condition = "'id' = 1 ";
$ Db-> update ('limit _ login', $ mod_content, $ condition );
} Else {
$ Mod_content = "'login _ times '= 'login _ times' + 1 ";
$ Condition = "'id' = 1 ";
$ Db-> update ('limit _ login', $ mod_content, $ condition );
}
/* Prompt the user for several more opportunities */
If ($ row ['login _ times '] <3 ){
$ Row ['login _ times '] ++; // here, the number of logins increases by 1, which is the same as the reason described above because the connection cannot be synchronized.
Database;
Echo 'incorrect password! Error count: '. $ row ['login _ times']. '<br> ';
$ Remain_times = 3-$ row ['login _ times'];
Echo 'you still have'. $ remain_times. 'login opportunity ';
}
}
} Else {
Echo 'user name and password cannot be blank! ';
}
}



?>

<Form action = "" method = "post">
Username: <input type = "text" name = "username" value = "" maxlength = "40"/> <br>
Password: <input type = "password" name = "pw"/> <br>

<Input type = "submit" name = "submit" value = "login"/>

</Form> 1 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.