gethostbyaddr() returns only numerical IPs :(

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zzap
Forum Newbie
Posts: 2
Joined: Mon Jul 19, 2004 11:59 am

gethostbyaddr() returns only numerical IPs :(

Post 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
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post 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
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

See if this works instead:

Code: Select all

$ip = '198.182.196.56';
$host = `host $ip`;
echo $host;
zzap
Forum Newbie
Posts: 2
Joined: Mon Jul 19, 2004 11:59 am

Post 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.
Post Reply