我正在尝试制作一个脚本,我可以在其中检查服务器状态(在线/离线),例如:Teamspeek、Minecraft 等。
我找到了这个脚本:
<?php
function getStatus($ip,$port){
$socket = @fsockopen($ip, $port, $errorNo, $errorStr, 3);
if(!$socket) return "offline";
else return "online";
}
echo getStatus("server.com", "80");
?>
但是我不必更改服务器名称和端口,而是从 mysql 数据库中获取它。所以我做了这个,但我的问题是:我无法将字符串连接到脚本。我获取字符串的代码如下所示:
<?php
// Check to see the URL variable is set and that it exists in the database
if (isset($_GET['id'])) {
// Connect to the MySQL database
include "connect_mysql.php";
$id = preg_replace('#[^0-9]#i', '', $_GET['id']);
// Use this var to check to see if this ID exists, if yes then get the product
// details, if no then exit this script and give message why
$sql = mysql_query("SELECT * FROM servers WHERE id='$id' LIMIT 1");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
// get all the product details
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$ip = $row["ip"];
$port = $row["port"];
$getit = $row["getit"];
$name = $row["name"];
$content = $row["content"];
}
} else {
echo "That item does not exist.";
exit();
}
}
?>
所以我想我可以改变echo getStatus("server.com", "80");
Toecho getStatus("$ip", "$port");
但这不起作用。有没有人知道我必须做什么?