I have this simple php code fired by Ajax that checks if an entered password is correct. Now for this function I would also want to have an ability to block login for 10 seconds after 5 failed attempts in entering.
Here is the code itself:
//Beforehand I started the session, and connected the $link to the mysql database
if(!isset($_SESSION['lim'])){
$_SESSION['lim'] = 5;
}
$p = $_POST['text']; //I WILL TAKE CARE OF VALIDATION LATER
//All of the passwords will be crypted, and the variable will be
//validated. Just after I'm done with this system.
if($_SESSION['lim']>0){
$result = mysqli_query($link, "SELECT id FROM passwd where pass = '".$p."' ;");
if( mysqli_num_rows($result) > 0){
echo 1;
$_SESSION["passed"] = "true";
}else{
echo 0;
--$_SESSION['lim'];
}
}else{
echo 2;
if(!isset($_SESSION['blockTime'])){$_SESSION['blockTime'] = time();}
}
if(($_SESSION['lim'] == 0) && (time() - $_SESSION['blockTime'] > 10)){
$_SESSION['lim'] = 5;
}
The problem is the code doesn't do the blocking part after 5 tries. Now the issue itself is most probably located in this bit:
if(($_SESSION['lim'] == 0) && (time() - $_SESSION['blockTime'] > 10)){
$_SESSION['lim'] = 5;
}
Because without this portion, the blocking works fine (apart from not enabling the login after 10 seconds). It seems the if statement is executed everytime instead of only when the lim is empty and 10 seconds have passed.
What could possibly be the issue here and how could I make the if work properly