PHP服务器ping似乎导致502错误

时间:2013-08-30 10:17:40

标签: php mysql ping minecraft

我设置了一个cron jon来浏览我的数据库,通过整个循环获取Minecraft服务器的ip和端口,然后ping它以获得有多少在线,然后存储到数据库中。

除了我认为有一台服务器可能处于脱机状态,因此我认为脚本会在它到达时挂起。我的问题是如何防止这种情况并停止发生的502错误;

502 Bad Gateway 服务器返回了无效或不完整的响应。

status.class.php {https://github.com/FunnyItsElmo/PHP-Minecraft-Server-Status-Query/blob/master/MinecraftServerStatus/status.class.php}

我的剧本:

<?php
require('../config.php');
require ('../lib/status.class.php');

$SQL = mysql_query("SELECT id, ip, port FROM servers");
while($row = mysql_fetch_assoc($SQL)){
//Fetch the server info
$id     = $row['id'];
$ip     = $row['ip'];
$port   = $row['port'];
echo $id.' - '.$ip.' - '.$port;

$PSQL = mysql_query("SELECT * FROM server_ping WHERE server_id = '$id' AND pinged >= now() - INTERVAL 5 MINUTE");
$Pnum = mysql_num_rows($PSQL);

if($Pnum == 0){
    //If the server was not pinged within the last 5 minutes
    //Ping the server
    $status = new MinecraftServerStatus();
    $response = $status->getStatus($ip, $port);
    if($response) {
        $current    = $response['players'];
        $max        = $response['maxplayers'];
    } else {
        $current    = '--';
        $max        = '--';
    }

    //Check if the server is in the list
    $BSQL = mysql_query("SELECT * FROM server_ping WHERE server_id = '$id'");
    $Bnum = mysql_num_rows($BSQL);
    if($Bnum == 0){
        //If the server has never been added - add it
        $ASQL = mysql_query("INSERT INTO server_ping (server_id, current, max) 
            VALUES ('$id', '$current', '$max')");

    } else {
        //If the server has been added - update it
        $ASQL = mysql_query("UPDATE server_ping SET current = '$current', max = '$max' WHERE server_id = '$id'");
    }

} else {
    //If the server was pinged within the last 5 minutes
}
}
?>

编辑: 有趣的是,我通过设置$ current = 99进行了一些故障排除,然后运行脚本将所有服务器设置为99 /等等。但该页面在...之后仍然返回502?

2 个答案:

答案 0 :(得分:0)

要全面分析您的问题,您需要一个像http://en.wikipedia.org/wiki/Wireshark这样的工具,并遵循502发生的原因。

此脚本已包含“离线检测”。我不认为这是离线/在线的问题。

答案 1 :(得分:0)

如果您使用的是php-fpm,可以尝试重新启动它。