Umair Khan
Umair Khan

Reputation: 1752

PHP - Validate NS / DNS Server Ip using dns_get_record()

I am trying to validate NS / DNS Server IP provided by user, using dns_get_record().

Code:

$site = 'google.com';
$ns = array('104.28.4.30'); // Some Ip Address not NS / DNS
echo '<pre>';
$result = dns_get_record($site, DNS_A, $ns);
print_r($result);
print_r($ns);

Output:

Array
(
[0] => Array
    (
        [host] => google.com
        [class] => IN
        [ttl] => 299
        [type] => A
        [ip] => 74.125.200.113
    )

...

[5] => Array
    (
        [host] => google.com
        [class] => IN
        [ttl] => 299
        [type] => A
        [ip] => 74.125.200.101
    )
)
Array
(
)

As I have provided some random IP but dns_get_record() seems to fetch records somehow regardless whether the provided IP is NS / DNS or not.

PS. I am aware of NET_DNS2 class but thought if there is a work around in native PHP.

Upvotes: 1

Views: 1723

Answers (1)

Barmar
Barmar

Reputation: 782181

The third argument to dns_get_record() is an output parameter, the initial value is ignored. It's filled in by the function with the authoritative servers for the domain.

However, DNS servers don't always return the list of authoritative servers in their responses, this is a configurable option in most nameservers (including BIND, the most common nameserver). If the server doesn't return anything in the Authority Section of the response, the array will be empty.

Upvotes: 2

Related Questions