emtecif emtecif - 6 months ago 16
PHP Question

Incorrectly executed script for the selection of IP addresses

I wrote a small script to remove unwanted IP addresses from the list, but script still displays IP addresses from

$ip_list
, although in condition
if
nothing happens

$ip_list = array("37.13.","217.118.","207.46.");
$top = $pdo->prepare('SELECT DISTINCT ip FROM logs ORDER BY ip DESC');
$top->execute();
while ($toppost = $top->fetch(PDO::FETCH_LAZY)){
$array[] = $toppost->ip;
}



for ($i = 0; $i <= count($ip_list); $i++){
for ($a = 0; $a <= count($array); $a++){
$b = explode(".", $array[$a]);
$n = $b[0].".".$b[1].".";
if ($ip_list[$i] == $n){

}
else{
echo $n."<br/>";
}
}
}

Answer

This should work :

PHP

$ip_list = array("37.13.","217.118.","207.46.");
$top = $pdo->prepare('SELECT DISTINCT ip FROM logs ORDER BY ip DESC');
$top->execute();
while ($toppost = $top->fetch(PDO::FETCH_LAZY)){
    $b = explode(".", $toppost->ip);
    $formated_ip = $b[0].".".$b[1].".";
    if (in_array($formated_ip, $ip_list)) {
        //IP From database in $ip_list array    
    } else {
        echo $formated_ip . "<br/>";
    }
}