Home PHP Network checkdnsrr() PHP checkdnsrr() Function Definition In PHP, checkdnsrr() asks DNS whether a hostname (or IP) has at least one record of a given type. It returns true when a matching record is found and false when none are found or the lookup fails.
The default record type is MX (mail exchanger). You can also ask for A, AAAA, NS, TXT and other common types. This is a quick yes-or-no check, not a full dump of the records.
Syntax Call checkdnsrr() with the values you need. Start with this line.
$ok = checkdnsrr("example.com", "MX"); Call What it does checkdnsrr("example.com")Looks for MX records. Returns true if any exist. checkdnsrr("example.com", "A")Looks for IPv4 A records for the hostname. checkdnsrr("127.0.0.1", "PTR")Looks for a reverse PTR record for an IP address.
checkdnsrr() only answers yes or no. For the actual record data, use dns_get_record() or getmxrr(). On some Windows setups DNS functions need a working resolver; always treat false as "not found or lookup failed".
Library declarations checkdnsrr(string $hostname, string $type = "MX"): bool Parameters Parameter Description hostname Required. The host name or IP address to look up in DNS. type Optional. The DNS record type to search for, such as "MX", "A", "AAAA", "NS", "TXT" or "ANY". The default is "MX".
Return Value Return Value Description bool true if at least one matching DNS record is found; false if none are found or an error occurred.
PHP Examples 1. Check for MX Records 2. Check Several Record Types 3. Guard Before Looking Up Mail Hosts Example 1 — Check for MX Records Ask whether a domain has MX records. The output below is a typical sample result, not a live lookup.
<?php
$host = "example.com";
if (checkdnsrr($host, "MX")) {
echo $host, " has MX records.", PHP_EOL;
} else {
echo $host, " has no MX records (or lookup failed).", PHP_EOL;
} example.com has MX records. How It Works We store the domain name in $host. checkdnsrr() looks for MX records for that host. true means at least one MX record was found. The printed line is a typical demo result for a well-known domain. Example 2 — Check Several Record Types Loop over common types and print whether each one is present. Sample answers are fixed for the tutorial.
<?php
$host = "example.com";
$types = ["A", "AAAA", "MX", "NS", "TXT"];
foreach ($types as $type) {
$found = checkdnsrr($host, $type);
echo $type, ": ", $found ? "yes" : "no", PHP_EOL;
} A: yes
AAAA: yes
MX: yes
NS: yes
TXT: yes How It Works The array lists common DNS record types. Each loop calls checkdnsrr() with a different $type. We print "yes" or "no" based on the boolean return value. Real answers depend on the domain and your DNS server; this output is a typical sample. Frequently Asked Questions What does checkdnsrr() do in PHP? checkdnsrr() checks DNS for records of a given type for a hostname or IP. It returns true if any matching records exist, otherwise false.
What is the default type for checkdnsrr()? The default $type is "MX", so checkdnsrr("example.com") looks for mail exchanger records.
How is checkdnsrr() different from dns_get_record()? checkdnsrr() only returns true or false. dns_get_record() returns the actual record data as arrays, or false on failure.
Is dns_check_record() the same as checkdnsrr()? Yes. dns_check_record() is an alias of checkdnsrr(). Both names call the same function.
Can I use checkdnsrr() to validate an email address? No. It only checks DNS records for a domain. It does not prove that an inbox exists or that mail will be accepted.
Next: closelog() closelog() closes the connection to the system logger that was opened with openlog(). Afte.
closelog() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion