我试图集成一个php脚本,如果特定服务器处于脱机状态或在线状态,每30秒检查一次,然后appropriatley打印我的Drupal 7.23网站上的状态。
我想出了下面的代码,但php脚本报告服务器一直处于脱机状态,即使它处于联机状态。我不知道是什么问题。
<div class="serverstatus">
<?php
ini_set( "display_errors", 0); //hide fsockopen/fopen warnings if file doesn't exist or couldn't connect
$g_Status = 0;
$g_Ip = "0.0.0.0"; //Server ip
$g_Port = "0000"; //Server Port
function IsOnline($ip, $port)
{
$sock=@fsockopen($ip, $port, $errNo, $errStr, 3);//timeout set to 3 seconds
if($sock)
{
fclose($sock);
return 1;
}
return 0;
}
function RefreshStatus()
{
global $g_Ip, $g_Port;
$status = IsOnline($g_Ip, $g_Port);
//storing info about timestamp and server status
$file = fopen("status.txt", "wb");
$timestamp = time() + 30; //it will refresh every 30 seconds - won't flood the server
$cont = $timestamp .' '. $status;
fwrite($file, $cont);
fclose($file);
return $status;
}
$file = fopen("status.txt", "r");
if(!$file)
{
//file doesn't exist
$g_Status = RefreshStatus();
}else
{
$cont = fread($file, filesize("status.txt"));
$data = explode(" ", $cont); //$data[0] is our timestamp and $data[1] is our server status
if($data[0] < time())
{
//refresh status
$g_Status = RefreshStatus();
}else
{
$g_Status = $data[1];
}
}
//Display server status
if($g_Status)
{
echo "Online";
}else
{
echo "Offline";
}
?>
</div>
我非常感谢所有答案!谢谢你,并致以最诚挚的问候。
答案 0 :(得分:0)
好吧,我让它工作,以便准确报告服务器。我还是不确定出了什么问题但很好,它的方式更简单,如果有人想知道如何连接drupal 7与Lineage 2专用服务器来检查状态,这里是代码:
<div class="serverstatus">
<?php
$server = "server ip";
$portg = "game server port";
$portl = "login server port";
$timeout = "1";
$game = @fsockopen("$server", $portg, $errno, $errstr, $timeout);
$login = @fsockopen("$server", $portl, $errno, $errstr, $timeout);
echo "Login Server: ";
echo $login ? "<font color=\"green\">OnLine</font>" : "<font color=\"red\">Off line</font>";
echo "<br>Game Server: ";
echo $game ? "<font color=\"green\">OnLine</font>" : "<font color=\"red\">Off line</font>";
?>
</div>
感谢所有的答案,问候。