PHP Ping工具第二个If或Else语句

时间:2016-09-14 08:36:02

标签: php html

我是网络开发的新手,我正在制作一个php ping工具来查看我们有哪些网站。因为我们有115个站点,所以我选择只显示已关闭的站点。在没有网站关闭或不回复ping的网页上,我可以显示一条消息,上面写着“没有网站没有网站”等信息。这是带有bootstrap样式的php。

$counter = "";
foreach ($host1 as $value1) 

{

 $counter = $counter + 1;
  echo "<tr>"; 
  echo '<body bgcolor="#FFFFFF" text="#000000"></body>';       
  //check target IP or domain
  $pingreply = exec("ping -n $count $value1");
  if ( substr($pingreply, -2) == 'ms')
    {

    }
  else 
    {
        #echo "<td width=60><strong><font color='#990000'>DOWN</font></strong></td>";
        echo "<p><fstrong textont size='6'><center><strong><div class='alert alert-danger'>" . $services1[$counter] . " - No Reply</div></strong></center></font></p>";
    }
}

是否有人能够帮助我,我不知道如何在此代码中添加第二个else或第二个if语句。

1 个答案:

答案 0 :(得分:0)

正如我在评论中所说:

$counter = "";
$has_down = false;   // flag, no sites are down yet
foreach ($host1 as $value1) 
{
  $counter = $counter + 1;
  echo "<tr>"; 
  echo '<body bgcolor="#FFFFFF" text="#000000"></body>';       
  //check target IP or domain
  $pingreply = exec("ping -n $count $value1");
  if ( substr($pingreply, -2) == 'ms')
    {

    }
  else 
    {
        #echo "<td width=60><strong><font color='#990000'>DOWN</font></strong></td>";
        echo "<p><fstrong textont size='6'><center><strong><div class='alert alert-danger'>" . $services1[$counter] . " - No Reply</div></strong></center></font></p>";
        // site down, set flag
        $has_down = true;
    }
}

// flag still false, all sites work
if (!$has_down) {
    echo 'There are no sites down';
}