PHP中实现网络ping的功能,可以直接调用操作系统的ping命令。例如在Linux下,可以这样实现:
<?php
// 参照ping命令的参数
exec("ping -c 2 www.tudaxia.com", $output, $result);
if ($result == 0) {
//Ping successful!\n";
//print_r($output) . "\n";
$ping_sum = $output[count($output)-2];
// 为了确认网络畅通,这里检查的条件有些苛刻:0丢失!
$pos = strpos($ping_sum, "0% packet loss");
if ($pos >-1 ) {
echo "server is active \n";
}
}