1. php
  2. /references
  3. /checkdnsrr

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)
ParameterDescriptionOptional/Required
$hostnameThe hostname to check (e.g. webreference.com)Required
$typeThe 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 TypeDescription
AIPv4 address
AAAAIPv6 address
CNAMECanonical name for an alias
MXMail exchange record for email routing
NSAuthoritative name server for the domain
PTRPointer to a canonical name for a host
SOAStart of authority record for the domain, containing primary DNS details
SRVService record, used to identify servers for specified services
TXTText 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";
}
?>

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