You do not want to use the database for the "number of failed login attempts"? Then just use a cookie and check it out. Of course, they can remove it, but this is a hassle.
However , I suspect that you are already getting the username and password from the database, why not get the last number of failed logins while you are on it?
if (isset($_POST['submit_login'])) {
if (isset($_POST['username']) && isset($_POST['password'])) {
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$rs = mysql_query('SELECT id,Username,Password,Failed_logins,IP_address FROM Users WHERE Username = '.$username.'');
$num = mysql_num_rows($rs);
if ($num > 0) {
$row = mysql_fetch_array($rs);
if ($password == $row['Password']) {
mysql_query('UPDATE Users SET Failed_logins = 0 WHERE id = '.$row['id'].'');
header('location: success.php');
} else {
if ($row['Failed_logins'] > 3) {
header('location: captcha.php');
} else {
$ip = $_SERVER['REMOTE_ADDR'];
if ($row['IP_address'] != $ip) {
$failed_logins = 0;
} else {
$failed_logins = $row['Failed_logins']+1;
}
mysql_query('UPDATE Users SET Failed_logins = '.$failed_logins.',IP_address = '.$ip.' WHERE id = '.$row['id'].' ');
}
}
} else {
$error = 'no_such_username';
}
} else {
$error = 'incomplete_form';
}
}
Something like that.
EDIT:
This is really old code, and I see some problems with it now. But at least you should always use PDOs (prepared instructions) to insert data into your database.
source
share