PHP 5.6.0 released

checkdnsrr

(PHP 4, PHP 5)

checkdnsrrRésolution DNS d'une adresse IP

Description

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

Recherche l'enregistrement DNS de type type correspondant à l'hôte host.

Liste de paramètres

host

host peut être soit une adresse IP au format numérique, soit un nom d'hôte.

type

type peut être l'une des valeurs suivantes : A, MX, NS, SOA, PTR, CNAME, AAAA, A6, SRV, NAPTR, TXT ou ANY.

Valeurs de retour

Retourne TRUE si un enregistrement a été trouvé, et FALSE en cas d'erreur ou si aucun enregistrement n'a été trouvé.

Historique

Version Description
5.3.0 Cette fonction est maintenant disponible sous Windows.
5.2.4 Le type TXT a été ajouté.
5.0.0 Le type AAAA a été ajouté.

Notes

Note:

Pour une compatibilité avec Windows avant son implémentation, essayez la classe » PEAR : » Net_DNS.

Voir aussi

  • dns_get_record() - Lit les données DNS associées à un hôte
  • getmxrr() - Retourne les enregistrements MX d'un hôte
  • gethostbyaddr() - Retourne le nom d'hôte correspondant à une IP
  • gethostbyname() - Retourne l'adresse IPv4 correspondant à un hôte
  • gethostbynamel() - Retourne la liste d'IPv4 correspondante à un hôte
  • la page du manuel man named(8)

add a note add a note

User Contributed Notes 7 notes

up
10
Patrick
9 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
10 months 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
7 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