Click to See Complete Forum and Search --> : Simple Array Problem


96turnerri
11-24-2004, 05:50 AM
hi am having problems with this it just doesnt work


<?php

$ip = $_SERVER["REMOTE_ADDR"];
$banned = file("ips.php");

$num = 0;
while(array_key_exists($num, $banned)) {
if($banned[$num] == $ip) {
exit("You Have Been Banned From This Chat Room1");
} else {
echo $banned[$num]."<br>";
}
$num++;
}

if(in_array($ip, $banned)) {
exit("You Have Been Banned From This Chat Room2");
}

echo $ip."<br>";
print_r($banned);

?>


but why? simple simple php, i have just coded a 78kB command file for this and now i come to do something comparativly trivial 'IP blocking' its not working, any ideas?

http://www.richardturner.com/chat/ip.php

silent11
11-24-2004, 08:00 AM
from the docs:

[quote]
Note: Each line in the resulting array will include the line ending, so you still need to use rtrim() if you do not want the line ending present.
[quote]


http://us2.php.net/function.file


you may need to use this as 168.12.124.21\n does not equal 168.12.124.21 .

96turnerri
11-24-2004, 08:06 AM
thanks, didnt know that, learn something new everyday ;)

final code
<?php
$ip = $_SERVER["REMOTE_ADDR"]."\n";
$banned = file("ips.php");
if(in_array($ip, $banned)) {
exit("You Have Been Banned From This Chat Room");
}
?>

silent11
11-24-2004, 08:49 AM
...learn something new everyday

Me too, PHP has more built-in global functions than you can shake a stick at.