1

I'm trying to compare IP addresses in a list against a list of blocked IPs. If the IP address is equal to an address in the $blockedIP array, the row is not included in the list. The list is populated from a mysqli_query. What I would like to do is exclude the whole row if the IP address matches one in the array.

echo '<div class="genericTable"><h2>Downloaded Sermons Report</h2><table>';

$blockedIP = array('69.58.178.58', '173.199.115.123', '173.199.120.99');

foreach($blockedIP as $ip){
$sql = mysqli_query($db, "SELECT * FROM dl_log WHERE ipaddress <> '".$ip."'");

while($row = mysqli_fetch_assoc($sql)){
    echo '<tr>';

    if($row['ipaddress'] !== $ip){
        foreach($row as $each){
            echo '<td class="genericTable">'.$each.'</td>';
        }
    }
    echo '</tr>';
  }
}
echo '</table></div>';

I've tried the script a few different ways and either get the whole list or every row & column are compared to the array, which makes for a jacked up looking table. I'm using the foreach to compare values to the array. Where should I put it? Thanks.

3
  • you know blocking IP's, is pointless right? IP != user. One IP could be million of people, and one person could use a new IP for every request. Commented Dec 18, 2012 at 19:24
  • Well, if you want to avoid a row, just do your check before printing the row... Commented Dec 18, 2012 at 19:24
  • Dagon, I'm not blocking IPs from access, just from showing up on a list. The entire script analyzes IPs of users that have downloaded files, when they were downloaded, etc. The blocked IPs appear to be search engine robots. I don't want to bloat the table with unnecessary data. Commented Dec 18, 2012 at 19:42

2 Answers 2

2

What I would like to do is exclude the whole row if the IP address matches one in the array.

You have a few options:

  1. One query using SQL IN. Condense the blocked IP address with implode().

    SELECT * FROM dl_log WHERE ipaddress NOT IN (...);
    
  2. One query and use PHP in_array() to filter he results

    // SQL
    SELECT * FROM dl_log;
    
    // PHP
    if (!in_array($row['ipaddress'], $blockedIP))
    
Sign up to request clarification or add additional context in comments.

1 Comment

and if its search engines you want to stop, use robots.txt file or useragent analysis . far more efficient, bot ips change all the time
0

If you want to not print the row out, change

if($row['ipaddress'] !== $ip){

to

if(!in_array($row['ipaddress'], $blockedIP)){

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.