PHP Function Reference

PHP checkdnsrr() Function

The PHP checkdnsrr() function searches DNS for records of specified type corresponding to the specified hostname. The function returns true if records of specified type are found, otherwise returns false.


checkdnsrr(hostname, type)


hostname Required. Specify an IP address or host name to check.
type Optional. Specify the record type to check. It can be any one of the following:
  • A
  • MX (default)
  • NS
  • SOA
  • PTR
  • AAAA
  • A6
  • SRV
  • TXT
  • ANY

Return Value

Returns true if any records are found; returns false if no records were found or if an error occurred.


The example below shows the usage of checkdnsrr() function.


//checking for 'MX' records
if(checkdnsrr($domain, "MX")){
  echo "Records exists.";
} else {
  echo "Records do not exist or an error occurred.";

The output of the above code will be:

Records exists.


Consider one more example where other record types are checked.


$types = ["A", "MX", "NS", "SOA", "PTR", 
          "CNAME", "AAAA", "A6", "SRV", 
          "NAPTR", "TXT", "ANY"];

//checking for record types
foreach($types as $i){
  if(checkdnsrr($domain, $i)){
    echo "$i : found \n";
  } else {
    echo "$i : Not found \n";

The output of the above code will be:

A : found 
MX : found 
NS : found 
SOA : found 
PTR : Not found 
CNAME : Not found 
AAAA : Not found 
A6 : Not found 
SRV : Not found 
NAPTR : Not found 
TXT : found 
ANY : found 

❮ PHP Network Reference