Dwza
Dwza

Reputation: 6565

Using gethostbyname to resolve an IP

I want to code some debug-windows that only shows up when I am visiting the page. I have a DDNS address that contains my IP. This IP is asked on the server. Or lets say it should be asked. But there comes the resolving problem.

I tried to resolve an IP by using:

echo gethostbyname("google.com");

No matter what domain I use, it always fails and only returns the domain as documented. It returns the IPv4 address or a string containing the unmodified hostname on failure.

I also tried:

function getAddrByHost($host, $timeout = 3) {
    $query = "nslookup -timeout=$timeout -retry=1 $host";
    if(preg_match('/\nAddress: (.*)\n/', $query, $matches))
        return trim($matches[1]);
    return $host;
}
echo getAddrByHost("google.com");

This one has the same result. Performing nslookup -timeout=$timeout -retry=1 google.com in my commandline on my pc works fine. But I need it for a script so this doesn't help me at all :)

I expect a normal IPv4 address.

Does this function need special php.ini settings to run?

My main question is, is this function totally broken?

Upvotes: 2

Views: 947

Answers (1)

Devon Bessemer
Devon Bessemer

Reputation: 35337

  1. Be sure to check the ability to connect to port 53 (DNS)
  2. You can debug further by trying passthru('nslookup google.com 2>&1') from PHP. This will show the full output from stdout and stderr as it's likely to include some more info.

Upvotes: 2

Related Questions