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.

Syntax

checkdnsrr(hostname, type)

Parameters

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
  • CNAME
  • AAAA
  • A6
  • SRV
  • NAPTR
  • TXT
  • ANY

Return Value

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

Example:

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

<?php
$domain="alphacodingskills.com";

//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 similar to:

Records exists.

Example:

Consider one more example where other record types are checked.

<?php
$domain="alphacodingskills.com";

$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 similar to:

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