<?php
$value = $_SERVER['REMOTE_ADDR'];
function confirmIPAddress($value) {
$query = "SELECT
attempts, (CASE when lasttry is not NULL
AND
DATE_ADD(lasttry, INTERVAL " . TIME_PERIOD . " MINUTE)>NOW() then 1 else 0 end) as denied
FROM
" . TBL_ATTEMPTS . "
WHERE
ip = '" . $value . "'";
$result = mysql_query($query) OR die(mysql_error());
$data = mysql_fetch_array($result);
if(!$data) {
return 0;
}
if($data['attempts'] >= ATTEMPTS_NUMBER) {
if($data['denied'] == 1) {
return 1;
}else{
$this->clearLoginAttempts($value);
return 0;
}
}
return 0;
}
function addLoginAttempt($value) {
$query = "SELECT * FROM " . TBL_ATTEMPTS . " WHERE ip = '" . $value . "'";
$result = mysql_query($query) OR die(mysql_error());
$data = mysql_fetch_array($result);
if($data) {
$attempts = $data['attempts']+1;
if($attempts == 3) {
$query = "UPDATE " . TBL_ATTEMPTS . " SET attempts=" . $attempts . ", lasttry=NOW() WHERE ip = '" . $value . "'";
$result = mysql_query($query) OR die(mysql_error());
}else{
$query = "UPDATE " . TBL_ATTEMPTS . " SET attempts=" . $attempts . " WHERE ip = '" . $value . "'";
$result = mysql_query($query) OR die(mysql_error());
}
}else{
$query = "INSERT INTO " . TBL_ATTEMPTS . " (attempts,IP,lasttry) VALUES(1, '" . $value . "', NOW())";
$result = mysql_query($query) OR die(mysql_error());
}
}
function clearLoginAttempts($value) {
$query = "UPDATE " . TBL_ATTEMPTS . " SET attempts = 0 WHERE ip = '" . $value . "'";
return mysql_query($query) OR die(mysql_error());
}
?>