Reputation: 752
I tried to make an ip based deny list which stored ip numbers in mysql. Basicly i try to header redirect if user's ip in array but it wont work. What is wrong?
$ip_array = array();
$ip_ban_query = mysql_query("SELECT ip FROM banned_ips");
while ($deny = mysql_fetch_assoc($ip_ban_query)){
$add_ip = $deny['ip'];
$ip_array[] = $add_ip;
}
if (in_array ($_SERVER['REMOTE_ADDR'], $ip_array)) {
header("Location: http://www.google.com/");
exit();
}
Upvotes: 0
Views: 198
Reputation: 3319
We can greatly simplify your code here.. reducing complexity almost always flushes away bugs. :)
// There are several different methods to accomplish this, and you really
// should be using statements here, but both are out of scope of this question.
$ipBanQuery = sprintf("SELECT ip FROM banned_ips WHERE ip = '%s'", mysql_real_escape_string($_SERVER['REMOTE_ADDR']));
$result = mysql_query($ipBanQuery);
if (mysql_num_rows($result)) {
header('Location: http://www.google.com/');
exit();
}
It also depends on where you're calling this code. Be extra-sure that this is being called before any output to the browser - stray spaces, HTML, or other debugging info will prevent any additional headers from being sent. Check your webserver's error log to see if there's something wonky going on.
Upvotes: 1