Open In App

PHP | dns_check_record() Function

The dns_check_record() function is an inbuilt function in PHP which is used to check the DNS records corresponding to the hostname or IP address. This function can be used to verify whether a domain name exists or not.

Note: This function is an alias of the checkdnsrr() function.



Syntax:  

bool dns_check_record( string $host, string $type )

Parameters: This function accepts two parameters as mentioned above and described below: 



Return Value: This function returns TRUE if records found, otherwise returns FALSE.

Note: 

Below programs illustrate the dns_check_record() function in PHP:

Program 1:  




<?php
 
$domain = "geeksforgeeks.org";
 
if(dns_check_record($domain, "MX")) {
    echo "Record exists.";
} else {
    echo "Record not found or error occurred.";
}
?>

Output: 

Record exists.

Program 2: 




<?php
 
$domain = "geeksforgeeks.org";
 
$arr = array(
    "A", "MX", "NS", "SOA",
    "PTR", "CNAME", "AAAA", "A6",
    "SRV", "NAPTR", "TXT", "ANY"
);
 
foreach( $arr as $element) {
    echo $element . ":";
     
    if(dns_check_record($domain, $element)) {
        echo "found <br>";
    } else {
        echo "not found <br>";
    }
}
 
?>

Output: 

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

Reference: https://www.php.net/manual/en/function.dns-check-record.php
 


Article Tags :