Page 1 of 1

gethostbyaddr() returns only numerical IPs :(

Posted: Mon Jul 19, 2004 11:59 am
by zzap
Hi all,

I am trying to resolve IP addresses into their hostnames using gethostbyaddr().

The weird thing is that I get different results if I run php from a shell than if I access the script through the web server.

More specifically, the following lines in an executable file:

#!/usr/local/bin/php
<?
print( gethostbyaddr( "198.182.196.56" ) );
?>

... will print out "www.linux.org". If I stick the <?...?> block into a .php file and access it through my browser, I get "198.182.196.56".

I'm pretty lost here.... any suggestions appreciated.

I'm running Apache 1.3.29, mod_php 4.3.4 statically linked, FreeBSD 5.2

thanks
antoine

Posted: Mon Jul 19, 2004 12:26 pm
by Joe
I just tested your exact code and got no error on my server at all. Perhaps something within your server is not setup right. Try making a small change and write:

<?php
$host = gethostbyaddr("198.182.196.56");
echo $host
?>

*edit* And there is no need to write #!/usr/local/bin/php

Posted: Mon Jul 19, 2004 12:32 pm
by kettle_drum
See if this works instead:

Code: Select all

$ip = '198.182.196.56';
$host = `host $ip`;
echo $host;

Posted: Tue Jul 20, 2004 5:13 am
by zzap
Joe -- yes, I guess something is not set up right, since the CLI version of php returns the hostname OK while the apache module returns the numerical IP.

kettle_drum -- thanks for the suggestion, but I was hoping to be able to use native php functions. Otherwise you add the overhead of starting a shell to run the 'host' command every time a request gets served.

Also, on my system 'host' returns:
user@search:~#host 198.182.196.56
56.196.182.198.IN-ADDR.ARPA domain name pointer http://www.linux.org

So I'd have the extra overhead of doing a regexp to extract the actual hostname from that string. I've read the man page and there are no switches that allow you to just return the hostname without all the crap at the start of the line.

What really bugs me is that gethostbyaddr() works _fine_ from within a shell script, but behaves differently within apache.