How to check domain nameservers in PHP?

12,064

Solution 1

You should simple use dns_get_record function and optionally you can pass DNS_NS as second parameter.

For your domain zinas.lv using

var_dump(dns_get_record('zinas.lv', DNS_NS));

I get:

array(2) { [0]=> array(5) { ["host"]=> string(8) "zinas.lv" ["class"]=> string(2) "IN" ["ttl"]=> int(1655) ["type"]=> string(2) "NS" ["target"]=> string(10) "ns1.dns.lv" } [1]=> array(5) { ["host"]=> string(8) "zinas.lv" ["class"]=> string(2) "IN" ["ttl"]=> int(1655) ["type"]=> string(2) "NS" ["target"]=> string(10) "ns2.dns.lv" } }

So you can simply display those 2 dns using:

$dns = dns_get_record('zinas.lv', DNS_NS);

echo $dns[0]['target'].' '.$dns[1]['target'];

Solution 2

Use this function to get the nameserver's of any domain as an array:

function getDns($domain)
{
    $dns = dns_get_record($domain, DNS_NS);
    $nameservers = [];
    foreach ($dns as $current)
        $nameservers[] = $current['target'];
    return $nameservers;
}
Share:
12,064
Justas Pukys
Author by

Justas Pukys

Updated on June 13, 2022

Comments

  • Justas Pukys
    Justas Pukys almost 2 years

    I need check what nameservers domain is using, but can't find correct solution in PHP.

    I have tried checkdnsrr(); and dns_get_record();, both of them do not show NS for some of domains that are working. Whois is also not a solution.

    My purpose is to filter domains which have nameserver set and which haven't. Is there any solution for this?