0

I trying to create simple site in php that show hosts if they are ONLINE or DOWN. If the host is online I got background in table green if it is down it changes to red, plus it show me ping in ms.

I should like to get one extra function. Example of what i want:

if hosts ping is 0-200ms - green background
if hosts ping is 201-999ms - yellow background
if host ping is 1000ms and bigger - red background

I can't code so much but I try to learn. Here is my code I use at the moment with online-offline option:

    <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>

Hello again, I have changed to exec and i got to work ONLINE and OFFLINE status but i cant figure out how to create funktion that will give me information based on ping time. Now i tryed to add funktion in script that show me if ping is: high or good or down but i cant get it work.

    <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 ("&nbsp;");        
    echo $pingTime[2];
    echo ("&nbsp;");
    $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>
  • `fSockOpen` is actually [fsockopen](http://php.net/manual/ro/function.fsockopen.php) – Alex Tartan Jul 08 '17 at 20:16
  • I don't think that you have to use sockets at all, you should use the `ping` command through [`exec`](http://php.net/manual/en/function.exec.php) or [**shell_exec**](http://php.net/manual/en/function.shell-exec.php). Also checkout this question: https://stackoverflow.com/questions/7093860/php-shell-exec-vs-exec – SaidbakR Jul 08 '17 at 20:22
  • Thanks , I have changed to exec and i got to work ONLINE and OFFLINE status but i cant figure out how to create funktion that will give me information based on ping time. Now i tryed to add funktion in script that show me if ping is: high or good or down but i cant get it work. New code is in my question i edit. – Jan Gajdosik Jul 13 '17 at 00:29

0 Answers0