PHP 8.5.0 Alpha 1 available for testing

Voting

: max(eight, eight)?
(Example: nine)

The Note You're Voting On

Stuart Macdonald
14 years ago
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)
{
// there was an error performing the command
}
else
{
$x=0;
while (
$x < (sizeof($output)))
{
$string.= $output[$x];
$x++;
}
}

if (empty(
$string))
$string = $ip;
else
//remove the trailing dot
$string = substr($string, 0, -1);

return
$string;
}
?>

<< Back to user notes page

To Top