用PHP ping Minecraft服务器:小错误

时间:2013-02-01 19:31:48

标签: php html minecraft

我正在尝试将Minecraft服务器中的数据输入到我的网页中作为测试。这是代码:

<?php $host = "hub.cobalt-mc.com";
$port = 25565;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$connected = socket_connect($socket, $host, $port);
if ($connected) {
$ping_start = microtime(true);
socket_send($socket, "\xFE", 1, 0);
$data = "";
$result = socket_recv($socket, &$data, 1024, 0);$ping_end = microtime(true);
socket_close($socket);

if ($result != false && substr($data, 0, 1) == "\xFF") {
$info = explode("\xA7", mb_convert_encoding(substr($data,1), "iso-8859-1","utf-16be"));
$serverName = substr($info[0], 1);
$playersOnline = $info[1];
$playersMax = $info[2];
$ping = round(($ping_end - $ping_start) * 1000);
echo     "<table class=\"table table-hover\">
<th>Server Address</th>
<th>Status</th>
<th>Rank</th>
<th>Total DP</th>

<tr>
<td>$host:$ip</td>
if (($ping > '1000') || ($ping < '0')) {
echo "<td><span class=\"label label-success\">Online!</span></td>";
}
else {
echo "<td><span class=\"label label-success\">Online!</span></td>";
}
<td></td>
<td></td>
</tr>

<tr>
<td>$host:$ip</td>
<td></td>
<td></td>
<td></td>
</tr>
</table>";
                            }

 else {
 echo "<span style=\"color:#FF0000\">Error: </span>Failed to receive data";
 }
 } else {
 echo "<span style=\"color:#FF0000\">Error: </span>Failed to send data";
 }?>

我收到错误:

Parse error: syntax error, unexpected '>' in /home/mctronco/public_html/leaderboards/serverping.php on line 27

代码有问题吗?

3 个答案:

答案 0 :(得分:0)

你的回声是在$ping = round(...行没有在if语句之前终止之后开始的......你不能只在if中间放一个echo

这一行:

<td>$host:$ip</td>

应该是:

<td>$host:$ip</td>";

然后你需要在if语句之后再做一个回声......

在该代码中似乎存在无数其他问题会妨碍您做您想做的事情......

正如评论中所述,正确的代码格式化将大大有助于解决这些错误...使用编辑器为代码着色,这样的错误会跳出来......

答案 1 :(得分:0)

您的错误消息引用第27行:

Parse error: syntax error, unexpected '>' in /home/mctronco/public_html/leaderboards/serverping.php on line 27

所以目前尚不清楚具体哪一行存在问题。因此,我建议您更改所有内容,因为您的编码非常简单。这不是我的理想,但应该有效。

if ($result != false && substr($data, 0, 1) == "\xFF") {
$info = explode("\xA7", mb_convert_encoding(substr($data,1), "iso-8859-1","utf-16be"));
$serverName = substr($info[0], 1);
$playersOnline = $info[1];
$playersMax = $info[2];
$ping = round(($ping_end - $ping_start) * 1000);

echo "<table class=\"table table-hover\">";
echo "<th>Server Address</th>";
echo "<th>Status</th>";
echo "<th>Rank</th>";
echo "<th>Total DP</th>";

echo "<tr>";
echo "<td>$host:$ip</td>";
if (($ping > '1000') || ($ping < '0')) {
echo "<td><span class=\"label label-success\">Online!</span></td>";
}
else {
echo "<td><span class=\"label label-success\">Online!</span></td>";
}
echo "<td></td>";
echo "<td></td>";
echo "</tr>";

echo "<tr>";
echo "<td>".$host.":".$ip."</td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "</tr>";
echo "</table>";
                            }

 else {
 echo "<span style=\"color:#FF0000\">Error: </span>Failed to receive data";
 }
 } else {
 echo "<span style=\"color:#FF0000\">Error: </span>Failed to send data";
 }

?>

答案 2 :(得分:0)

您的代码中存在多个问题,但我要指出一个重要问题。你不能把纯PHP放在字符串或echo的中间。

旧代码

<tr>
<td>$host:$ip</td>
if (($ping > '1000') || ($ping < '0')) {
echo "<td><span class=\"label label-success\">Online!</span></td>";
}
else {
echo "<td><span class=\"label label-success\">Online!</span></td>";
}
<td></td>

新代码

<tr>
<td>{$host}:{$ip}</td>";

if (($ping > '1000') || ($ping < '0')) {
echo "<td><span class=\"label label-success\">Online!</span></td>";
}
else {
echo "<td><span class=\"label label-success\">Online!</span></td>";
}

echo "
<td></td>

如果在echo之外执行if语句将其结果存储在变量中,然后将变量放在echo中,那会更好。当你在双引号内(比如你的回声),你可以让PHP显示变量的内容,但它不会执行语句。