1

I have an array of IP addresses that I use with in_array to deny people in the following code.

$deny = array("111.222.333.444","999.555.444.222");
if (in_array ($_SERVER['REMOTE_ADDR'], $deny)) {
header("location: http://google.com/");
exit();
}

Is there an alternative to in_array using a database. If I have a database of IP addresses how can I check the database for the IP address and redirect using the header?

1
  • Just query the database with the ip of the user and if you get a result deny access. Commented Dec 25, 2011 at 22:56

1 Answer 1

1

Assuming MySQLi:

$query = "SELECT count(*) AS denied
          FROM table_of_ips
          WHERE ip = '".mysqli_real_escape_string($link, $_SERVER['REMOTE_ADDR'])."'
          LIMIT 1";
$result = mysqli_fetch_assoc(mysqli_query($link, $query));

if ($result['denied']) {
  header("location: http://google.com/");
  exit;  
}

In essence, just SELECT where the IP matches the client IP, if you get a result, it's denied.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.