Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
 
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. –  Dagon Dec 18 '12 at 19:24
 
Well, if you want to avoid a row, just do your check before printing the row... –  Damien Pirsy Dec 18 '12 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. –  timbocf Dec 18 '12 at 19:42

2 Answers

up vote 1 down vote accepted

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))
    
share|improve this answer
 
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 –  Dagon Dec 18 '12 at 19:30
1  
The second one worked. Thanks! –  timbocf Dec 18 '12 at 19:37

If you want to not print the row out, change

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

to

if(!in_array($row['ipaddress'], $blockedIP)){
share|improve this answer
 
Ah, perfect. Thanks! –  timbocf Dec 18 '12 at 19:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.