2017-07-17 3 views
1

スクリプトが正常に動作していますが、ホストがpingに応答しないか、要求がタイムアウトになった場合、私のサーバーからエラー500が発生します。PHP Pingスクリプトエラー500

可能であれば、私のPHPコード内でこの問題を解決したいと思います。

ホストがpingでタイムアウトした場合、単に「タイムアウト」と表示され、サーバーからのエラーではありません。

<html> 
     <head> 
     <meta http-equiv="refresh" content="30" > 
<?php 
error_reporting(0); // Turn off all error reporting 
?> 
     </head> 
     <body bgcolor="#222222"> 
     <center> 
     <table width="100%" height="" border="1" cellpadding="3" style="color:black"> 
     <tr>  



<td align = center width="150" 
<?php 
function icmp_checksum($data) { 
    if (strlen($data) % 2) { 
    $data .= "\x00"; 
    } 
    $bit = unpack('n*', $data); 
    $sum = array_sum($bit); 
    while ($sum >> 16) { 
    $sum = ($sum >> 16) + ($sum & 0xffff); 
    } 
    return pack('n*', ~$sum); 
} 
function ping($host) { 

    $tmp = "\x08\x00\x00\x00\x00\x00\x00\x00PingTest"; 
    $checksum = icmp_checksum($tmp); 
    $package = "\x08\x00".$checksum."\x00\x00\x00\x00PingTest"; 
    $socket = socket_create(AF_INET, SOCK_RAW, 1); 
    socket_connect($socket, $host, null); 
    $timer = microtime(1); 
    socket_send($socket, $package, strlen($package), 0); 
    if (socket_read($socket, 255)) { 
    return round((microtime(1) - $timer) * 1000, 2); 
    } 
} 

$host="telia.se"; 
$pingtime = ping ($host); 

if ($pingtime == FALSE) 
    echo "bgcolor='red" ."' >"; 
else if ($pingtime > 40) 
    echo "bgcolor='yellow" ."' >"; 
else 
    echo "bgcolor='lime" ."' >"; 
echo ("<h2><B>$host</b></h2>"); 
echo $pingtime."ms"; 
?> 
</td> 

      </tr> 
      </table> 
      </center> 
      </body> 

    </html> 
+0

正確なエラーは何ですか? –

+0

エラー500 内部サーバーエラー –

答えて

0

あなたの関数は、pingが成功すると値を返すだけです。失敗した場合に値を返すように更新します。

if (socket_read($socket, 255)) { 
    return round((microtime(1) - $timer) * 1000, 2); 
} else { 
    return false; 
}