PHP中主机的状态(在线高电平)
我试图用php创建一个简单的站点,显示主机是否在线或停机。如果主机在线,我的背景显示为绿色,如果主机关闭,背景显示为红色,加上以毫秒显示的ping 我想要一个额外的功能。我想要的示例: 如果主机ping为0-200ms-绿色背景PHP中主机的状态(在线高电平),php,ping,host,Php,Ping,Host,我试图用php创建一个简单的站点,显示主机是否在线或停机。如果主机在线,我的背景显示为绿色,如果主机关闭,背景显示为红色,加上以毫秒显示的ping 我想要一个额外的功能。我想要的示例: 如果主机ping为0-200ms-绿色背景 如果主机ping为201-999ms-黄色背景 如果主机ping大于等于1000ms-红色背景 我不会写那么多代码,但我会努力学习。以下是我目前使用的带有“联机-脱机”选项的代码: <html> <head>
如果主机ping为201-999ms-黄色背景
如果主机ping大于等于1000ms-红色背景 我不会写那么多代码,但我会努力学习。以下是我目前使用的带有“联机-脱机”选项的代码:
<html>
<head>
<meta http-equiv="refresh" content="30" >
<?php
error_reporting(0); // Turn off all error reporting
?>
<?php
function ping($host, $port, $timeout) {
$tB = microtime(true);
$fP = fSockOpen($host, $port, $errno, $errstr, $timeout);
if (!$fP) { return "--"; }
$tA = microtime(true);
return round((($tA - $tB) * 1000), 0)." ms";
}
if( $state == "local" || $state == "testing" )
{
ini_set( "display_errors", "1" );
error_reporting( E_ALL & ~E_NOTICE );
}
else
{
error_reporting( 0 );
}
?>
</head>
<body bgcolor="#222222">
<center>
<table width="200" border="1" cellpadding="3" style="color:black">
<tr>
<td align =center
<?php
if (!$socket = @fsockopen("google.com", 80, $errno, $errstr, 1))
{
echo "bgcolor='red" . $last_file[0] . "' alt='error' >";
echo "OFFLINE"; }
else
{
echo "bgcolor='green" . $last_file[0] . "' alt='error' >";
fclose($socket);
echo "ONLINE"; }
?>
<br>
google.com
<br>
<?php echo ping("google.com", 80, 1);?>
</td>
</tr>
</table>
</center></font>
</body>
</html>
fSockOpen
实际上我不认为您必须使用套接字,您应该通过或使用ping
命令。还要检查这个问题:谢谢,我已经改为exec,我可以在线和离线工作,但我不知道如何创建函数,根据ping时间为我提供信息。现在我尝试在脚本中添加funktion,告诉我ping是高还是好还是低,但我无法让它工作。新代码在我编辑的问题中。
<html>
<head>
<meta http-equiv="refresh" content="30" >
<?php
error_reporting(0); // Turn off all error reporting
?>
</head>
<body bgcolor="#222222">
<center>
<table width="100%" height="" border="1" cellpadding="3" style="color:black">
<tr>
<td align =center
<?php
$host="google.se";
exec("ping -i 1 " . $host, $output, $result);
if ($result == 0)
echo "bgcolor='lime" . "' >";
else
echo "bgcolor='red" . "' >";
echo ("<h2><B>google.se</b></h2>");
if ($result == 0)
echo nl2br ("ONLINE\n");
else
echo nl2br ("OFFLINE\n");
$ping = exec("ping $host");
$pingTime = explode(',',trim($ping));
echo (" ");
echo $pingTime[2];
echo (" ");
$time = explode("=",trim($pingTime[2]));
if ($pingTime == 0)
echo 'host is down';
else if ($ping > 150)
echo 'high ping';
else
echo 'ping is good';
?>
</td>
</tr>
</table>
</center></font>
</body>
</html>