PHP checkdnsrr() Function
PHP's checkdnsrr()
is used to check if a domain has a specific resource record (e.g. MX, A, CNAME, etc.) in its DNS records. It is typically used to verify if a domain has a valid DNS configuration, or to check if a domain is able to receive email.
<?php
// Check if webreference.com has an MX record
if (checkdnsrr("webreference.com", "MX")) {
echo "webreference.com has an MX record";
} else {
echo "webreference.com does not have an MX record";
}
?>
Syntax and Parameters
checkdnsrr($hostname, $type)
Parameter | Description | Optional/Required |
---|---|---|
$hostname | The hostname to check (e.g. webreference.com) | Required |
$type | The type of resource record to check for (e.g. MX, A) | Optional |
The function will return true
if the domain has the specified resource record, and false
if it does not.
The $type
parameter can be any of the following values:
Resource Record Type | Description |
---|---|
A | IPv4 address |
AAAA | IPv6 address |
CNAME | Canonical name for an alias |
MX | Mail exchange record for email routing |
NS | Authoritative name server for the domain |
PTR | Pointer to a canonical name for a host |
SOA | Start of authority record for the domain, containing primary DNS details |
SRV | Service record, used to identify servers for specified services |
TXT | Text record, used to store arbitrary text data |
Examples
Check if webreference.com has an A record
<?php
if (checkdnsrr("webreference.com", "A")) {
echo "webreference.com has an A record";
} else {
echo "webreference.com does not have an A record";
}
?>
Check if webreference.com has a CNAME record
<?php
if (checkdnsrr("webreference.com", "CNAME")) {
echo "webreference.com has a CNAME record";
} else {
echo "webreference.com does not have a CNAME record";
}
?>
Related functions:
dns_get_record()
- retrieves DNS resource records for a given hostname
getmxrr()
- retrieves MX records for a given hostname
gethostbyname()
- resolves a hostname to its IP address