Search code examples
phpdnslookupnameservers

How to check domain nameservers in PHP?


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?


Solution

  • 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'];