Skip to content

Instantly share code, notes, and snippets.

@kamermans
Created January 2, 2012 01:32
Show Gist options
  • Select an option

  • Save kamermans/1548922 to your computer and use it in GitHub Desktop.

Select an option

Save kamermans/1548922 to your computer and use it in GitHub Desktop.
Simple DNSBL/RBL PHP function - trust me, it's better than checkdnsrr, fsock, socket_create, Net::DNSBL and Net::DNS
<?php
// Simple DNSBL/RBL PHP function - trust me, it's better than checkdnsrr, fsock, socket_create, Net::DNSBL and Net::DNS
// Here's a [better] way to quickly check if an IP is in a DNSBL / RBL. It works on Windows and Linux,
// assuming nslookup is installed. It also supports timeout in seconds.
function ipInDnsBlacklist($ip, $server, $timeout=1) {
$response = array();
$host = implode(".", array_reverse(explode('.', $ip))).'.'.$server.'.';
$cmd = sprintf('nslookup -type=A -timeout=%d %s 2>&1', $timeout, escapeshellarg($host));
@exec($cmd, $response);
// The first 3 lines (0-2) of output are not the DNS response
for ($i=3; $i<count($response); $i++) {
if (strpos(trim($response[$i]), 'Name:') === 0) {
return true;
}
}
return false;
}
ipInDnsBlacklist('188.163.68.29', 'dnsbl.tornevall.org'); // true
ipInDnsBlacklist('127.0.0.1', 'dnsbl.tornevall.org'); // false
?>
@AlexMcowkin
Copy link
Copy Markdown

hm...
for me its not work...

any IP always FALSE

@kamermans
Copy link
Copy Markdown
Author

You probably don't have nslookup in your environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment