Php user login code (limit the number of user logon errors) (1/2) _ PHP Tutorial

Source: Internet
Author: User
Php user login code (limit the number of user logon errors) (12 ). Php user login code (limit the number of wrong user logins) php Tutorial user login code (limit the number of wrong user logins) session_start (); include (mysql tutorial. class. php); $ dbnewmy 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.' seconds passed
';
// 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'
Related Article

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.