W3docs

PHP checkdnsrr() Function: Everything You Need to Know

As a PHP developer, you may need to verify domain names or check whether a domain name exists. In such scenarios, the PHP checkdnsrr() function comes in handy.

As a PHP developer, you may need to verify domain names or check whether specific DNS records exist. The checkdnsrr() function is a built-in PHP function designed for this purpose. It checks whether a specific type of DNS record exists for a given domain name and returns true if it does, or false otherwise.

⚠️ Important: checkdnsrr() was deprecated in PHP 8.2 and removed in PHP 8.4. For modern PHP applications, use dns_get_record() instead. This article covers checkdnsrr() for legacy code maintenance and educational purposes.

In this article, we will take an in-depth look at the checkdnsrr() function and its usage.

What is the checkdnsrr() Function?

The checkdnsrr() function is a PHP built-in function that allows you to check the DNS records of a domain name. It checks whether a specific type of DNS record exists for the given domain name. The function returns a boolean value, which is true if the DNS record exists, and false otherwise.

How to Use the checkdnsrr() Function

Using the checkdnsrr() function is straightforward. Here is the syntax of the function:

Syntax of the checkdnsrr() Function

checkdnsrr($host, $type);

The function takes two parameters:

  • $host: The domain name that you want to check.
  • $type: The type of DNS record that you want to check. This parameter is optional and defaults to "MX" if not specified.

Here is an example of how to use the checkdnsrr() function to check the DNS records of a domain name:

How to use PHP checkdnsrr() Function?

<?php

$domain = "example.com";
if (checkdnsrr($domain)) {
    echo "DNS record exists for $domain";
} else {
    echo "DNS record does not exist for $domain";
}

In this example, we check whether a DNS record exists for the domain "example.com". The function returns true if the DNS record exists and false otherwise. Note that DNS queries can fail due to network issues, timeouts, or invalid hostnames. In production, consider wrapping the call in error suppression (@checkdnsrr()) or using dns_get_record() for robust error handling.

Types of DNS Records

The checkdnsrr() function can check various types of DNS records. The $type parameter specifies the type of DNS record that you want to check. Here are some of the most common types of DNS records:

  • A: Returns the IPv4 address of the domain name.
  • AAAA: Returns the IPv6 address of the domain name.
  • MX: Returns the mail exchange server for the domain name.
  • NS: Returns the name server for the domain name.
  • CNAME: Returns the canonical name for an alias.

Conclusion

The checkdnsrr() function is a useful tool for verifying domain names and checking whether a domain name exists. By understanding the syntax and usage of the function, you can easily check various types of DNS records for a domain name. We hope this article has been informative and useful in understanding the checkdnsrr() function in PHP.

Practice

Practice

What does the checkdnsrr() function in PHP do?