0

I have a database table that has the IPs of users who visit the website. I'm trying to create an IP ban system for a page. I want the IPs that that are listed as blocked from my database table to be listed in an array of IPs that cannot access this particular page. I keep getting errors and I can't figure out how to get it to work properly. This is what i'm using so far:

PHP:

$getBlockedUsers = mysqli_query($con,"SELECT ip FROM blog_comments WHERE blocked='yes'");
$separateIps = implode(", ", mysqli_fetch_array($getBlockedUsers));

if (in_array ($_SERVER['REMOTE_ADDR'], $separateIps)) {
   header("location: http://www.google.com");
   exit();
}
  • I'm connected to the database correctly
1
  • 2
    Why not just search for the row with ip = $_SERVER[REMOTE_ADDR]? Commented Jul 2, 2013 at 6:43

2 Answers 2

1
$getBlockedUsers = mysqli_query($con,"SELECT true FROM blog_comments WHERE blocked='yes' AND ip='" . $_SERVER['REMOTE_ADDR'] . "' LIMIT 1");

if ($getBlockedUsers->num_rows > 0) {
   header("location: http://www.google.com");
   exit();
}
Sign up to request clarification or add additional context in comments.

Comments

0

Remove the implode function,

$separateIps = mysqli_fetch_all($getBlockedUsers, MYSQL_NUM);

in_array expects parameter 2 to be an array. When using implode you will get a string instead of an array.

4 Comments

That's just one row of the table, not all the IPs.
@Barmar I am pretty sure he meant mysqli_fetch_all.
I believe Barmar is right. I'll need to test this. It has removed the errors. But I'm not sure if this will work for all IPs.
@PLB That won't work either, because it returns a multidimensional array.

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.