PHP dns_check_record() Function: Everything You Need to Know
As a PHP developer, you may need to verify DNS records or check whether a specific record exists for a domain. Historically, the dns_check_record() function was used for this purpose. However, please note that dns_check_record() was deprecated in PHP 7.3 and completely removed in PHP 8.0. This tutorial explains its legacy usage and provides the modern alternative, dns_get_record(), which is recommended for all current PHP projects.
What is the dns_check_record() Function?
The dns_check_record() function was a PHP built-in function that allowed you to check whether a specific type of DNS record existed for a given domain name. It returned a boolean value: true if the record existed, and false otherwise. Due to its removal in PHP 8.0, it is only available on legacy systems (PHP 7.2 and earlier).
How to Use the dns_check_record() Function
Using the legacy dns_check_record() function was straightforward. Here is its syntax:
The syntax of PHP dns_check_record() Function
dns_check_record($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.
Since dns_check_record() is no longer supported in modern PHP, use dns_get_record() instead. Here is how to check for a specific DNS record type:
How to Use the dns_get_record() Function?
<?php
$domain = "example.com";
$records = dns_get_record($domain, DNS_A);
if ($records) {
echo "DNS record exists for $domain";
} else {
echo "DNS record does not exist for $domain";
}In this example, we check whether an A record exists for the domain "example.com". The function returns an array of records if found, or false otherwise.
Types of DNS Records
Both dns_check_record() and dns_get_record() 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
While dns_check_record() was once a standard tool for verifying DNS records, it has been removed from modern PHP. By using the modern dns_get_record() alternative, you can reliably check various DNS record types for any domain. We hope this guide clarifies the legacy function and provides a practical path forward for current PHP development.
Practice
What does the function 'dns_check_record()' do in PHP?