PHP user login code (limit the number of incorrect user logins)
php tutorial user login code (limit the number of incorrect user logins)
/* User login operation */session_start();
include("mysql tutorial.class. php");
$db=new mysql('localhost', 'root', '', 'flx', 'conn', 'gbk');
// For the convenience of testing, here is just a simple definition A specific user is identified. In actual operation, the user information is derived 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();
/* Error Judgment of times exceeding the limit*/
// For users who have logged in more than three times, if the current time, minus the time when the login started, is less than a specific value,
will not be allowed to log in;
if ($username==user){
//Here the user can try to log in 3 times. The following is written as 2 because the page refresh is not synchronized with the database connection.
//That is to say, the second time When the error message is inserted into the database, the information obtained here is still the first time. The number of actual user errors has been increased once;
if($row['login_times']>2){
$current=time();
$passed_time=$current-$row['login_date'];
echo 'Now has passed'.$passed_time.' seconds
';
// Set the time to 900 seconds. During this time period, specific users cannot log in to the system; press
if(($current-$row['login_date'])<900){
//$test_pw =substr(md5(rand()),0,10);
//Now the admin (specific user) user has logged in incorrectly more than 3 times, then within the limited time,
//Even if his password Even if the input is correct, you cannot log in to the system;
if($pw==pw){
exit('You entered the wrong password more than three times, please log in to the system after 15 minutes!');
}
// Of course, the password entered by this user is not the correct password, and such a prompt is given;
if($pw!=pw){
exit('You entered the wrong password more than three times , please log in to the system after 15 minutes!');
}
}else{
} //The time limit has passed and this user can log in. Before this user logs in, To clear the last login time
;
$mod_content="`login_times`=0,`login_date`=0";
$condition="`id`=1";
$db->update('limit_login',$mod_content,$condition);
}
}
}
if($username&&$pw){
if($username==user&&$pw==pw){
/* User login is successful, */
// Update data table; table login The times and time are both set to zero;
$mod_content="`login_times`=0,`login_date`=0";
$condition="`id`=1";
$db-> update('limit_login',$mod_content,$condition);
echo '';
}else{
} //When the first error occurs, set the number of password errors to 1, and put the time when the session is saved 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);
} /*Reminder that there are still several opportunities for users*/
if ($ row ['login_times'] & lt; 3) {
$ row ['login_times'] ++; // Here The reason for adding 1 to the number of logins is the same as the reason mentioned above, which is due to the inability to synchronize the connection to the
database;
echo 'Wrong password! The number of errors is: '.$row['login_times'].' times< ;br> '; }else{
echo 'Username and password cannot be empty! ';
}
}
?> > username:
password:
1 2
http://www.bkjia.com/PHPjc/444885.html
www.bkjia.com
true