Here's a simple function that uses Dig to obtain the hostname for a given IP address. If no hostname can be found it returns the IP again.
Works only on linux / unix, or whatever other platform with dig installed as a command line utility.
<?php
function tryGetHost($ip)
{
$string = '';
exec("dig +short -x $ip 2>&1", $output, $retval);
if ($retval != 0)
{
}
else
{
$x=0;
while ($x < (sizeof($output)))
{
$string.= $output[$x];
$x++;
}
}
if (empty($string))
$string = $ip;
else $string = substr($string, 0, -1);
return $string;
}
?>