PHP 5.4.36 Released

checkdnsrr

(PHP 4, PHP 5)

checkdnsrrCheck DNS records corresponding to a given Internet host name or IP address

Opis

bool checkdnsrr ( string $host [, string $type = "MX" ] )

Searches DNS for records of type type corresponding to host.

Parametry

host

host may either be the IP address in dotted-quad notation or the host name.

type

type may be any one of: A, MX, NS, SOA, PTR, CNAME, AAAA, A6, SRV, NAPTR, TXT or ANY.

Zwracane wartości

Returns TRUE if any records are found; returns FALSE if no records were found or if an error occurred.

Rejestr zmian

Wersja Opis
5.3.0 This function is now available on Windows platforms.
5.2.4 TXT type was added.
5.0.0 AAAA type was added.

Notatki

Informacja:

For compatibility with Windows before this was implemented, then try the » PEAR class » Net_DNS.

Zobacz też:

  • dns_get_record() - Fetch DNS Resource Records associated with a hostname
  • getmxrr() - Get MX records corresponding to a given Internet host name
  • gethostbyaddr() - Get the Internet host name corresponding to a given IP address
  • gethostbyname() - Get the IPv4 address corresponding to a given Internet host name
  • gethostbynamel() - Get a list of IPv4 addresses corresponding to a given Internet host name
  • the named(8) manual page

add a note add a note

User Contributed Notes 7 notes

up
10
Patrick
10 years ago
This is a little code example that will validate an email address in two ways:
- first the general syntax of the string is checked with a regular expression
- then the domain substring (after the '@') is checked using the 'checkdnsrr' function

<?php

function validate_email($email){

  
$exp = "^[a-z\'0-9]+([._-][a-z\'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$";

   if(
eregi($exp,$email)){

      if(
checkdnsrr(array_pop(explode("@",$email)),"MX")){
        return
true;
      }else{
        return
false;
      }

   }else{

      return
false;

   }   
}

?>
up
1
criffoh at gmail dot com
1 year ago
Is not possible validate domains with 'ñ' for my country.

In my country is possible to register domain using 'ñ' character. For example:

ñandu.cl
http://nic.cl/cgi-bin/dom-CL?q=%F1andu

If I use this function to check DNS record, it return false, but the domain already exists:

var_dump(checkdnsrr('ñandu.cl', 'A')); // returns false
up
0
Krisztin Ferenczi
1 year ago
criffoh at gmail dot com is right. Before you check domain, you must convert to ascii with idn_to_ascii function:
http://us2.php.net/manual/en/function.idn-to-ascii.php .

var_dump(checkdnsrr('ñandu.cl', 'A')); // returns false
var_dump(checkdnsrr(idn_to_ascii('ñandu.cl'), 'A')); // return true
up
0
Anonymous
8 years ago
<?php
function check_dnsbl($ip)
{
   
$dnsbl_check=array("bl.spamcop.net","list.dsbl.org",
"sbl.spamhaus.org",'xbl.spamhaus.org');
    if(
$ip){
       
$rip=implode('.',array_reverse(explode(".",$ip)));
        foreach(
$dnsbl_check as $val){
            if(
checkdnsrr($rip.'.'.$val.'.','A'))
                return
$rip.'.'.$val;
        }
    }
    return
false;
}
?>
up
-1
satmd
8 years ago
fox dot 69 at gmx dot net: I wonder where you got this code from. I have written this piece of code half a year ago and released it WITH a copyright header that is missing now! Anyways... this code is to be considered licenced "as-is", however it'd be nice to keep the authors note (This is something about reputation, you see?). Thanks.

(Much) more recent version of it:
<?php
function is_blacklisted($ip) {
  
// written by satmd, do what you want with it, but keep the author please
  
$result=Array();
  
$dnsbl_check=array("bl.spamcop.net",
                      
"list.dsbl.org",
                      
"sbl.spamhaus.org");
   if (
$ip) {
      
$quads=explode(".",$ip);
      
$rip=$quads[3].".".$quads[2].".".$quads[1].".".$quads[0];
       for (
$i=0; $i<count($dnsbl_check); $i++) {
           if (
checkdnsrr($rip.".".$dnsbl_check[$i].".","A")) {
             
$result[]=Array($dnsbl_check[$i],$rip.".".$dnsbl_check[$i]);
           }
         }
      return
$result;
   }
}
?>

Beware that this code's signature differs from the original! I also removed osirusoft as its results are not useful anyways (false positives!). Please make sure that you have nscd or a caching dns server running as this code is prone to (d)dos! Only use it in places where it is necessary (when data is to be modified), e.g. the script processing uploads/posts/replies in a blog.
up
-1
fox dot 69 at gmx dot net
11 years ago
maybe usefull, a blacklist (DNSBL) check function:

<?php
function is_blacklisted($ip) {
   
$dnsbl_check=array("bl.spamcop.net",
                      
"relays.osirusoft.com",
                      
"list.dsbl.org",
                      
"sbl.spamhaus.org");
    if (
$ip) {
      
$quads=explode(".",$ip);
       
$rip=$quads[3].".".$quads[2].".".$quads[1].".".$quads[0];
        for (
$i=0; $i<count($dnsbl_check); $i++) {
            if (
checkdnsrr($rip.".".$dnsbl_check[$i],"A")) {
               
$listed.=$dnsbl_check[$i]." ";
            }
         }
       if (
$listed) { return $listed; } else { return FALSE; }
    }
}
?>
up
-1
kriek at jonkriek dot com
11 years ago
The checkdnsrr function is not implemented on the Windows platform. The way to get around this problem is to write your own version of checkdnsrr. Example: myCheckDNSRR

<?php
function myCheckDNSRR($hostName, $recType = '')
{
if(!empty(
$hostName)) {
   if(
$recType == '' ) $recType = "MX";
  
exec("nslookup -type=$recType $hostName", $result);
  
// check each line to find the one that starts with the host
   // name. If it exists then the function succeeded.
  
foreach ($result as $line) {
     if(
eregi("^$hostName",$line)) {
       return
true;
     }
   }
  
// otherwise there was no mail handler for the domain
  
return false;
}
return
false;
}
?>
Note that the type parameter is optional, and if you don't supply it then the type defaults to "MX" (which means Mail Exchange). If any records are found, the function returns TRUE. Otherwise, it returns FALSE.
To Top