Hi,
I have a small security function based off of 2 txt logs, one records failed attempts and the other records lockouts.
The script enters a line with the time and IP into the first txt file and then is supposed to count the occurrences of that IP that show up in the last 15 min, if its >= 3 then it will make an entry in the second one which is used earlier in the script before allowing the user access to the login page.
The problem I am having is getting array_keys to give me anything back when looking for the only IP in there right now...
Here is the function:
# # # # # # # # # # # # # # # # # # # # # # # #
# Login Failed
# # # # # # # # # # # # # # # # # # # # # # # #
function log_failed() {
global $uco;
$ip = $_SERVER['REMOTE_ADDR'];
# Write this failed attempt first
if(is_writable($uco->ptxt)) {
$string = time().','.$ip."\n";
file_put_contents($uco->ptxt,$string,FILE_APPEND);
} else {
echo("Script Error");
}
# Now read files to array
$b = $p = 0;
$btxt = file($uco->btxt);
$ptxt = file($uco->ptxt);
foreach($btxt as $bun) { list($btime[$b], $bip[$b]) = explode(",",$bun); $b++; }
foreach($ptxt as $pun) { list($ptime[$p], $pip[$p]) = explode(",",$pun); $p++; }
unset($b,$p);
# Count Instances of your ip in the txt files
if(is_array($pip)) {$y = array_keys($pip,$ip);}
if(is_array($bip)) {$z = array_keys($bip,$ip);}
$p=0;
if(is_array($y)) {
foreach($y as $a) {
echo $ptime[$a];
if($ptime[$a] > (time()-(15*60)))
{$p++;}
}
}
#this part is just to see where it breaks
echo $p."\n<br>\n";
print_r($pip);
echo "\n<br>\n";
print_r($y);
echo "\n<br>\n";
print_r(array_keys($pip,$ip));
echo "<br>\n".$ip;
# If 3 or more instances are from the last 15 minutes, add your name to the banlist
if($p >= 3) {
if(is_writable($uco->btxt)) {
$str = (time()+(15*60)).','.$ip."\n";
file_put_contents($uco->btxt,$str,FILE_APPEND);
$msg = "<br />3 failed attempts to login, you have been banned for 15 min";
} else {
$msg = "Script Error";
}
} else {
$msg = '';
}
return $msg;
}
and here is what it outputs:
0
<br>
Array
(
[0] => 65.32.58.50
[1] => 65.32.58.50
[2] => 65.32.58.50
[3] => 65.32.58.50
[4] => 65.32.58.50
[5] => 65.32.58.50
[6] => 65.32.58.50
[7] => 65.32.58.50
[8] => 65.32.58.50
[9] => 65.32.58.50
[10] => 65.32.58.50
[11] => 65.32.58.50
[12] => 65.32.58.50
[13] => 65.32.58.50
[14] => 65.32.58.50
[15] => 65.32.58.50
[16] => 65.32.58.50
[17] => 65.32.58.50
[18] => 65.32.58.50
[19] => 65.32.58.50
[20] => 65.32.58.50
[21] => 65.32.58.50
[22] => 65.32.58.50
[23] => 65.32.58.50
[24] => 65.32.58.50
[25] => 65.32.58.50
[26] => 65.32.58.50
[27] => 65.32.58.50
[28] => 65.32.58.50
[29] => 65.32.58.50
[30] => 65.32.58.50
[31] => 65.32.58.50
)
<br>
Array
(
)
<br>
Array
(
)
<br>
65.32.58.50
the one its searching for is the only one in there and it is in there 32x now 😕