Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP Network checkdnsrr() Function



The PHP Network checkdnsrr() function is used to find out if a specific DNS record for a domain name is present. DNS records include information about domains, like IP addresses and mail servers. This feature makes it easier to confirm if a domain is real or if it has particular DNS record types.

For example, you can use it to verify whether a domain is set up to distribute emails by searching for a "MX" record. If the record is present, the function returns true; if not, it returns false. It is commonly used for domain validation in web applications.

This method is simple to use and works well when domain-related tests are needed in PHP scripts.

Syntax

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

bool checkdnsrr ( string $hostname, string $type = "MX" )

Parameters

Here are the parameters of the checkdnsrr() function −

  • $hostname − (Required) It can be either the host name or the IP address in dotted-quad notation.

  • $type − (Optional) The type can be A, MX, NS, SOA, PTR, CNAME, AAAA, A6, SRV, NAPTR, TXT, or ANY.

Return Value

The checkdnsrr() function returns TRUE, if any records are found. Otherwise, it returns FALSE or an error occurs.

PHP Version

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

Example 1

Here is the basic program to show the usage of the PHP Network checkdnsrr() function. And it verifies that a domain has an MX (Mail Exchange) record, which is necessary for email services.

<?php
   // Check if the domain has an MX record
   $domain = "tutorialspoint.com";
   if (checkdnsrr($domain, "MX")) {
      echo "The domain '$domain' has an MX record.";
   } else {
      echo "The domain '$domain' does not have an MX record.";
   }
?>

Output

Here is the outcome of the following code −

The domain 'tutorialspoint.com' has an MX record.

Example 2

In this program we are validating multiple record types. This program basically checks for different DNS record types (A, MX, and CNAME) for a domain with the help of checkdnsrr() function.

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

   foreach ($types as $type) {
      if (checkdnsrr($domain, $type)) {
         echo "The domain '$domain' has a $type record.\n";
      } else {
         echo "The domain '$domain' does not have a $type record.\n";
      }
   }
?> 

Output

This will generate the below output −

The domain 'tutorialspoint.com' has a A record.
The domain 'tutorialspoint.com' has a MX record.
The domain 'tutorialspoint.com' does not have a CNAME record.

Example 3

Here we are using the checkdnsrr() function for domain validation with error handling. So it handles errors if the input is incorrect.

<?php
   // Validate domain with error handling
   function validateDomain($domain, $type = "MX") {
      if (filter_var($domain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) {
         if (checkdnsrr($domain, $type)) {
               echo "The domain '$domain' has a $type record.";
         } else {
               echo "The domain '$domain' does not have a $type record.";
         }
      } else {
         echo "Invalid domain format.";
      }
   }

   // Example usage
   validateDomain("tutorialspoint.com", "A");
?> 

Output

This will create the below output −

The domain 'tutorialspoint.com' has a A record.
php_function_reference.htm
Advertisements