简体   繁体   中英

IP Blacklist lookup with PHP

I am trying to count the number of hits an IP occur in the array function "dnsblookup". If the IP occurs three times, it should echo "3|10", where 10 is the total number of blacklist database in the array. My code returns "1|10" for every IP even if it occurs more than once in the blacklist database in the array. I do not know where I made the mistake. I need help to complete this project. Thank you. I put multiple asterisks (*) before the exact code I am having issues with.

    <?php

        function dnsbllookup($ip)
         {

            $dnsbl_lookup = array (
             "dnsbl-1.uceprotect.net","all.s5h.net","wormrbl.imp.ch",
             "dnsbl-2.uceprotect.net","blacklist.woody.ch",
             "dnsbl-3.uceprotect.net","combined.abuse.ch","dnsbl.spfbl.net",
             "dnsbl.dronebl.org","http.dnsbl.sorbs.net");

           $list = "";

           if ($ip) {
            $reverse_ip = implode(".", array_reverse(explode(".", $ip)));
            foreach ($dnsbl_lookup as $host) {
          if (checkdnsrr($reverse_ip . "." . $host . ".", "A")) {
            $list .= implode(".", array_reverse(explode(".",$reverse_ip)))   . ' <font color="red"> 
            <strong>is Listed in </strong></font>'. $host . '<br />';

            }
          }
       }

     if (empty($list)) {
      echo 'No record was not found in the IP blacklist database for:  '.$ip;
      echo  $list;
      echo "<br>";
      echo "0";
      echo "|";
      echo count($dnsbl_lookup);

      ********************
       } else {
       $list1 = explode(" ", $list);
       echo ($list);
       echo "<br>";
       echo "Blacklisted: ".count($list1);
       echo "|";
       echo count($dnsbl_lookup);
        }
      }

You should reduce the complexity and use string interpolation for easier reading and debugging.

function dnsbllookup($ip)
{
  $dnsbl_lookup =
    [
      'dnsbl-1.uceprotect.net', 'all.s5h.net', 'wormrbl.imp.ch',
      'dnsbl-2.uceprotect.net', 'blacklist.woody.ch',
      'dnsbl-3.uceprotect.net', 'combined.abuse.ch', 'dnsbl.spfbl.net',
      'dnsbl.dronebl.org', 'http.dnsbl.sorbs.net'
    ];

  $dns_count = count($dnsbl_lookup);

  $list = [];

  if ($ip)
  {
    $reverse_ip = implode(".", array_reverse(explode(".", $ip)));

    foreach ($dnsbl_lookup as $host)
      if (checkdnsrr("$reverse_ip.$host.", "A"))
        $list[] = "$ip <font color=\"red\"><strong>is Listed in </strong></font> $host<br />\n";
  }

  $list_count = count($list);

  if (0 === $list_count)
    echo "No record was not found in the IP blacklist database for:  $ip<br>\n";
  else
  {
    $list_str = implode(" ", $list);
    echo "$list_str<br>\nBlacklisted: $list_count<br>\n";
  }
  echo "$list_count|$dns_count\n";
}

dnsbllookup('54.201.153.20'); // 3 entries
dnsbllookup('185.12.225.17'); // 1 entry

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM