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, usedns_get_record()instead. This article coverscheckdnsrr()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?
checkdnsrr() (the name stands for "check DNS resource record") is a built-in PHP function that performs a live DNS lookup and reports whether a given domain has at least one record of a particular type. It is most often used to validate that an email address points at a domain that can actually receive mail, or to confirm that a host resolves before you attempt to connect to it.
The function returns a boolean: true if at least one matching record exists, false if none do or the lookup fails. Because it queries real DNS servers, the result reflects the live state of the domain — not a cached value in your code.
Syntax
checkdnsrr(string $hostname, string $type = "MX"): boolThe function takes two parameters:
$hostname— the domain name (or, in older PHP versions, an IP address) you want to check. You normally pass a bare domain such asw3docs.com, not a full URL.$type— the type of DNS record to look for. This parameter is optional and defaults to"MX"(mail records) when omitted.
Note: Because the default
$typeisMX, callingcheckdnsrr($domain)does not tell you whether the domain exists in general — only whether it has mail records. To check that a domain resolves at all, pass"A"(or"ANY").
How to Use the checkdnsrr() Function
Here is a basic example that checks whether a domain has mail (MX) records:
How to use PHP checkdnsrr() Function?
Because no $type is passed, this checks for MX records. The function returns true if at least one mail record exists and false otherwise. Keep in mind that DNS queries can fail due to network issues, timeouts, or invalid hostnames — a false result means "no record found or the lookup failed", so it is not proof that a domain is dead.
Types of DNS Records
The $type parameter specifies which kind of DNS record to look for. The most common values are:
| Type | What it checks |
|---|---|
A | IPv4 address of the host |
AAAA | IPv6 address of the host |
MX | Mail exchange (mail server) records — the default |
NS | Authoritative name servers for the domain |
CNAME | Canonical name (alias) records |
TXT | Free-form text records (SPF, verification tokens, etc.) |
SOA | Start-of-authority record |
ANY | Any of the above |
Checking a Specific Record Type
Pass the type as the second argument to look for something other than mail records. Here we confirm a domain resolves to an IPv4 address:
<?php
$domain = "w3docs.com";
if (checkdnsrr($domain, "A")) {
echo "$domain has an A record (it resolves to an IPv4 address).";
} else {
echo "$domain has no A record.";
}A Practical Use Case: Validating an Email Domain
A common real-world use is to sanity-check the domain part of an email address before sending mail to it. First validate the address format with filter_var(), then confirm the domain actually accepts mail:
<?php
function emailDomainHasMail(string $email): bool
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
return false;
}
$domain = substr(strrchr($email, "@"), 1);
// Fall back to A records: a host with only an A record can still receive mail.
return checkdnsrr($domain, "MX") || checkdnsrr($domain, "A");
}
var_dump(emailDomainHasMail("[email protected]")); // bool(true)
var_dump(emailDomainHasMail("[email protected]")); // bool(false)This will not tell you whether a specific mailbox exists, but it weeds out typos and made-up domains cheaply.
Common Gotchas
- The default type is
MX, not "any record". Callcheckdnsrr($domain, "A")(or"ANY") if you simply want to know whether a domain resolves. - Pass a hostname, not a URL. Strip the scheme and path first: use
w3docs.com, nothttps://w3docs.com/learn-php. - A
falseresult is not definitive. Network failures, slow resolvers, or temporary outages all returnfalse. Do not treat it as proof a domain is invalid in security-critical code. - It performs a live network call. Each invocation hits a DNS server, so avoid calling it in tight loops; cache results when validating many addresses.
Modern Alternative: dns_get_record()
checkdnsrr() was deprecated in PHP 8.2 and removed in PHP 8.4. In current code, prefer dns_get_record(), which returns the actual records instead of a bare boolean, giving you both existence and the data:
<?php
$domain = "w3docs.com";
$records = dns_get_record($domain, DNS_MX);
if (!empty($records)) {
echo "$domain has MX records.";
} else {
echo "$domain has no MX records.";
}For a focused look at retrieving mail records, see getmxrr() and dns_get_mx(). To resolve a hostname to its IP address, use gethostbyname(), and to do the reverse lookup, gethostbyaddr().
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.