James Hurford
James Hurford

Reputation: 2058

How do I work around dns_get_record returning false positives

I've been trying to use functions like dns_get_record($domain) to check the validity of a domain. The problem is if it's not a valid domain it appends it on front of the domain for the sites IP that I'm on. I can't rely on any of the gethostbyname, dns_get_record, etc functions for this as they don't work as I expect them to.

If a domain doesn't exist, I expect any of these functions return a failed result. Its not. I notice using the 'ping' command does the same thing as the PHP functions do, but the 'host' command works as expected.

I write a C program to use gethostbyname and it works as expected. Same with python 'socket.gethostbyname'. They both fail on a lookup with an invalid domain.

What is going on with the PHP functions? Why are they not behaving as I expect them too and fail on an invalid domain name?

using the following on a host with the domain name of example.com give the following.

dns_get_record('something')

Array
    (
        [0] => Array
            (
                [host] => somthing.example.com
                [type] => A
                [ip] => some ip address
                [class] => IN
                [ttl] => 600
            )

    )

This is not what I want. If there is a bug, a link to the bug would be appreciated, thanks

UPDATE:

As correctly pointed out by @Barmar, it's the way the DNS records have been set up. I found on my home computer, which has PHP on it, the function gethostbyname() and friends work exactly as expected.

This begs the question of how can you reliable check to see if a domain is valid when your DNS is setup to enable wildcards. The only way I found was to use command line tolls and call them from PHP. I don't like this option, but it's the only one I got at the moment, and it's not a frequently called operation anyway, luckily.

Upvotes: 3

Views: 2078

Answers (2)

user149341
user149341

Reputation:

You can disable the automatic search of local domains by appending a period to the domain you're searching for, e.g:

dns_get_record("something.")

Upvotes: 6

Barmar
Barmar

Reputation: 782226

This is because you have a wildcard record in your domain. When a name lookup fails, it gets retried with your local domain appended (based on the domain or search options in /etc/resolv.conf). Normally this fails, so the whole lookup fails. But if you have a wildcard in your domain, the retry succeeds, and this gets returned as the result.

Upvotes: 2

Related Questions