note 99648 added to function.gethostbyaddr

From: Date: Sat, 28 Aug 2010 12:07:08 +0000
Subject: note 99648 added to function.gethostbyaddr
Groups: php.notes 
Request: Send a blank email to php-notes+get-172131@lists.php.net to get a copy of this message
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; } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 77.249.150.248 ---- Manual Page -- http://www.php.net/manual/en/function.gethostbyaddr.php Edit -- https://master.php.net/note/edit/99648 Del: integrated -- https://master.php.net/note/delete/99648/integrated Del: useless -- https://master.php.net/note/delete/99648/useless Del: bad code -- https://master.php.net/note/delete/99648/bad+code Del: spam -- https://master.php.net/note/delete/99648/spam Del: non-english -- https://master.php.net/note/delete/99648/non-english Del: in docs -- https://master.php.net/note/delete/99648/in+docs Del: other reasons-- https://master.php.net/note/delete/99648 Reject -- https://master.php.net/note/reject/99648 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#172131) next »