PHP Network dns_check_record() Function



The PHP Network dns_check_record() function is used to check if a domain name or IP address has a specific DNS record. It helps verify that a website or domain actually exists. This function is same as the checkdnsrr() function. The function returns TRUE if the record can be found, and FALSE otherwise.

Syntax

Below is the syntax of the PHP Network dns_check_record() function −

bool dns_check_record( string $host, string $type )

Parameters

Here are the parameters of the dns_check_record() function −

  • $host − (Required) It is the domain name or IP you want to check.

  • $type − (Optional) It is the type of DNS record, like A, MX (default), CNAME, etc.

Return Value

The dns_check_record() function returns TRUE if the record is found, otherwise, it returns FALSE.

PHP Version

First introduced in core PHP 5, the dns_check_record() function continues to function easily in PHP 7, and PHP 8.

Example 1

First we will show you the basic example of the PHP Network dns_check_record() function to check if a domain has an MX (Mail Exchange) record, which shows it can receive emails.

<?php
   // Define domain here
   $domain = "example.com";

   // Check for MX record (default type)
   if (dns_check_record($domain, "MX")) {
      echo "The domain has an MX record.";
   } else {
      echo "No MX record found for the domain.";
   }
?>

Output

Here is the outcome of the following code −

The domain has an MX record.

Example 2

In the below PHP code we will check for a specific DNS record type. So we will try to use the dns_check_record() function and with the optional parameter $type's value A.

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

   // Check for A record (IP address)
   if (dns_check_record($domain, "A")) {
      echo "The domain has an A record.";
   } else {
      echo "No A record found for the domain.";
   }
?> 

Output

This will generate the below output −

The domain has an A record.

Example 3

Now the below code checks DNS records for an IP address. So we will check if an IP address has a PTR (Pointer) record which is typically used for reverse DNS lookups.

<?php
   $ip = "8.8.8.8";

   // Check for PTR record
   if (dns_check_record($ip, "PTR")) {
      echo "The IP address has a PTR record.";
   } else {
      echo "No PTR record found for the IP address.";
   }
?> 

Output

This will create the below output −

No PTR record found for the IP address.

Example 4

In the following example, we are using the dns_check_record() function to check and verify multiple DNS record types (MX, A, and CNAME) for the given domain.

<?php
   $domain = "example.com";
   $recordTypes = ["MX", "A", "CNAME"];

   foreach ($recordTypes as $type) {
      if (dns_check_record($domain, $type)) {
         echo "The domain has a $type record.\n";
      } else {
         echo "No $type record found for the domain.\n";
      }
   }
?> 

Output

Following is the output of the above code −

The domain has a MX record.
The domain has a A record.
No CNAME record found for the domain.
php_function_reference.htm
Advertisements