PHP – Ping an IP

The given PHP example ping an IP and returns approximate round trip time in milliseconds. 

Code Example:
<?php
function GetPing($ip=NULL) {
 if(empty($ip)) {$ip = $_SERVER&#91;'REMOTE_ADDR'&#93;;}
 if(getenv("OS")== "Windows_NT") {
  $exec = exec("ping -n 3 -l 64 ".$ip);
  return end(explode(" ", $exec ));
 }
 else {
  $exec = exec("ping -c 3 -s 64 -t 64 ".$ip);
  $array = explode("/", end(explode("=", $exec )) );
  return ceil($array&#91;1&#93;) . 'ms';
 }
}

echo GetPing("204.93.183.11");
?>

Post to Twitter Post to Digg Post to Facebook Post to Google Buzz Send Gmail

Leave a Comment

Your email address will not be published. Required fields are marked *