Checkdnssrr function in PHP

Check DNS records safely

checkdnsrr() can test whether a DNS record exists, while getmxrr() retrieves mail-exchanger hosts. DNS results are useful for validation signals but do not prove that an email address or remote service is usable.

<?php
$domain = 'example.com';

if (checkdnsrr($domain, 'MX')) {
    $hosts = [];
    $weights = [];
    getmxrr($domain, $hosts, $weights);
    print_r($hosts);
} else {
    echo 'No MX record found.';
}
?>

Current approach first; the original tutorial examples below are retained and cleaned up for continued learning and legacy maintenance.

We can check our DNS record to verify the existence of a domain name or not. We can see if plus2net.com is there in DNS record by using checkdnsrr function. This function returns True if available and returns False if not available.

The type can be A, MX, NS, PTR,SOA, AAAA, CNAME, SRV,A6 NAPTR, TXT or ANY

This function may not work properly in Windows platform ( Older than PHP 5.3 ).

Here is the simple code to check DNS records.
$domain="plus2net.com";
if(!checkdnsrr($domain,'MX')) {
echo "Not there ";
}else {
echo " It is there ";
}

MX record : getmxrr()

We can get details of mail exchanger record ( MX record ) by using getmxrr() function.
getmxrr(hostname, array $result, array $weight)
If returns True of False based on the information found. Output is stored in $result as array.
It takes optional parameter weight

Here is an example.
$domain='example.com';
if(getmxrr($domain,$mx_details)){
foreach($mx_details as $key=>$value){
echo "$key => $value <br>";
}
}

Example 1: Checking MX Records for a Domain

<?php
$domain = "plus2net.com";
if (!checkdnsrr($domain, 'MX')) {
    echo "Domain does not exist.";
} else {
    echo "Domain exists.";
}
?>

IP address and Geo Location of visitor Storing visitor details in MySQL table


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer