0

I have picked up a PHP ping tool and with help from another member here I have managed to get it to display everything I need correctly. The problem I'm having is this has to ping 115 different IP Addresses and some times gets the "Fatal Error maximum execution time of 30 seconds"

I have copied the code below and have only added 1 host and 1 service as to save space but there is normally 115 hosts and 115 services.

$host1[1] = "8.8.8.8";//These addresses are generic for security reasons.
$host1[2] = "8.8.8.8";
$host1[3] = "8.8.8.8";

$services1[1] = "site 1";//These site names are generic for security reasons.
$services1[2] = "site 2";
$services1[3] = "site 3";

    $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 "<p><font 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 "<p><font size='6'><center><strong><div class='alert alert-success'>All sites responding to pings</div></strong></center></font></p>";
        }

Where there is $host1 and $services1 I have 115 of these but I have only put 3 on the code here so it doesn't take up loads of space on the question.

Is there any way I can improve this as it is taking a long time to load the page and it is sometimes getting a fatal error if it takes to long.

Also I believe it is only sending 1 ping per $host but i'm wondering if I can get this to send 4 ping packets to site and if it gets an average ping response then the site is up, as we have some sites on ADSL Lines which often drop packets when pinging so when the site refreshes it says they are down but they have only dropped 1 ping.

Can we optimise this and increase the amount of pings to site?

Thanks for your help in advanced.

XSuperDan
  • 13
  • 1
  • 1
  • 4
  • Take a look at [this](http://stackoverflow.com/questions/9841635/how-can-i-ping-a-server-port-with-php#answer-16650339), i guess it's significantly faster than parsing system console output – Denis Sheremet Sep 14 '16 at 11:19
  • Thanks for the reply denis and i did look at the question before posting, I dont understand how to use that code and have it take 115 hosts and echo the result. Are you able to show me the code on how to do that? – XSuperDan Sep 14 '16 at 12:40
  • This also does not automate the process as i have to type the IP in to the created form for it to work. – XSuperDan Sep 14 '16 at 13:56
  • You may add function from my link to top of code and replace `$pingreply = exec("ping -n $count $value1");` with `$pingreply = ping($value1);` and `if ( substr($pingreply, -2) == 'ms')` with `if ($pingreply !== false)` in your code. – Denis Sheremet Sep 16 '16 at 04:59
  • Thanks for the reply denis, will this send 4 packets or 1 packet per ping? – XSuperDan Sep 16 '16 at 08:14
  • 1 packet, but you may simply call it 4 times instead of one time. Like `$pingreply = ping($value1) !== false || ping($value1) !== false || ping($value1) !== false || ping($value1) !== false;` – Denis Sheremet Sep 16 '16 at 08:21

0 Answers0