2つのファイルを作成します。
1:次のような出力データをpingするためのPHPスクリプト「ping.php」:
$ip = $_REQUEST['ip'];
$cache_file = "ping_{$ip}.json";
if(@filemtime($cache_file) >= (time() - 30)) {
echo file_get_contents($cache_file);
return ;
}
// cache is too old. now ping your server
$json = json_encode(Array(
'ip' => $ip,
'players' => $players_count
));
// save new cache
file_put_contents($cache_file, $json);
// output JSON
echo $result;
2:HTML + jQuery
<table id="servers">
<tr>
<td class="ip">1.2.3.4:1234</td>
<td class="players"></td>
</tr>
<tr>
<td class="ip">1.2.3.4:1234</td>
<td class="players"></td>
</tr>
<tr>
<td class="ip">1.2.3.4:1234</td>
<td class="players"></td>
</tr>
</table>
<script type="text/javascript">
$('#servers tr').each(function(){
var ip = $(this).find('td.ip').text();
var $players = $(this).find('td.players');
$players.html('loading');
// use ping.php to get count of players etc
$.get('ping.php?ip='+ip, function(data){
$players.html(data.players);
}});
</script>
サーバーごとにAJAXリクエストを使用するだけです。ping.php
サーバーのステータスを返します。