吴伟贤のBlog

Feed Rss

php 服务端执行ping 函数 测试平均速度

01.24.2013, php, by .

//php在服务端执行ping命令

//取得ping的平均速度 xms

function GetPing($ip=NULL) {
if(empty($ip)) {$ip = $_SERVER[‘REMOTE_ADDR’];}
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[1]) . ‘ms’;
}
}

echo GetPing(‘www.baidu.com’);

 

windows下要给php读cmd.exe的权限

评论已关闭。