【发布时间】:2013-09-03 09:27:01
【问题描述】:
我正在尝试集成一个 php 脚本,该脚本将每 30 秒检查一次特定服务器是否离线或在线,然后在我的 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>
我非常感谢所有的答案!谢谢你和最好的问候。
【问题讨论】:
-
与其抑制
@fsockopen的错误,为什么不删除@并找出错误? -
@BenFortune 我尝试删除@,但是drupal 没有报告任何错误。它说服务器处于离线状态,即使它不是。
-
是什么样的服务器?
-
@BenFortune 是天堂2的专用游戏服务器,有什么区别吗?
-
天哪,对不起。从 fsockopen 中删除 @ 后 drupal 有时会报告以下错误:Warning: fsockopen(): unable to connect to :0 (Failed to parse address "") in IsOnline() (第 101 行 C:\wamp\www\lineage2\网站\所有\主题\...\模板\page.tpl.php)。
标签: php html database drupal drupal-7