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");
だから私はToを変更できると思ったが、echo getStatus("$ip", "$port");
それはうまくいかない。私がしなければならないことを知っている人はいますか?