Check your server status - a basic ping

Check whether your server is up or down

In this tutorial I will show you how to check whether a domain, server is up or down and how fast it is. Generally speaking we will implement a ping command with PHP.

Step 1.

PHP has no built in function for the ping functionality so we need to find a way how to realize it. We want to know two information about the given domain. First of all we want to know whether it is available or not and as second it would be nice to get information about how fast the response is.

The main concept is that we try to connect to the server with socket connection on port 80 and measure the time how long does it take. To do this we will use the built in PHP function fsockopen() which opens Internet or Unix domain socket connection. On depends the return value we can decide whether the actual domain is available at the moment or not.

Let’s do some coding.



Step 2.

First we will implement the main function which will get a domain name as parameter and try to open it. The function will returns with the response time if the connection was success and -1 in other case. The function makes no input validation so it must be done before calling it. I will show it later.

Let’s see the main steps:

  • Save current time as startTime
  • Try to connect to the domain
  • Save current time again as stopTime
  • Check the return value
    • In case of error return with -1
    • In case of successfully connection calculate the time in ms and return with it.
The code looks like this:

<?php


// Function to check response time
function pingDomain($domain){
    
$starttime = microtime(true);
    
$file      = fsockopen ($domain, 80, $errno, $errstr, 10);
    
$stoptime  = microtime(true);
    
$status    = 0;

    if (!
$file) $status = -1;  // Site is down
    
else {
        
fclose($file);
        
$status = ($stoptime - $starttime) * 1000;
        
$status = floor($status);
    }
    return 
$status;
}
?>


Step 3.
Now let’s create some usable environment for our function. It means that we will create a HTML form where the visitor can define the domain name in a normal text field. As action parameter we will call our script again. I use no any special formatting and CSS to make this example as simple as it can be and focus only on the main topic.

The resulted form is quite simple:


      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="domain">
        Domain name:
        <table>
          <tr><td><input name="domainname" type="text" ></td></tr>
          <tr><td><input type="submit" name="submitBtn" value="Ping domain"></td></tr>
        </table>  
      </form>


During the form processing we need to validate the domain name. In this case it means that to be sure we remove the http:// prefix if it was submitted with the domain name. Here you can add more validation routines if you want. Now we can call the above implemented function with the given domain and display the result depending on the return value.

<?php    
    
// Check whether the for was submitted
    
if (isset($_POST['submitBtn'])){
        
$domainbase = (isset($_POST['domainname'])) ? $_POST['domainname'] : '';
        
$domainbase = str_replace("http://","",strtolower($domainbase));
        
        echo 
'<table>';

        
$status = pingDomain($domainbase);
        if (
$status != -1) echo "<tr><td>http://$domainbase is ALIVE ($status ms)</td><tr>";
        else               echo 
"<tr><td>http://$domainbase is DOWN</td><tr>";

         echo 
'</table>';
    }
?>


And we are all done.

The complete code looks like this:

<?php
// Function to check response time
function pingDomain($domain){
    
$starttime = microtime(true);
    
$file      = fsockopen ($domain, 80, $errno, $errstr, 10);
    
$stoptime  = microtime(true);
    
$status    = 0;

    if (!
$file) $status = -1;  // Site is down
    
else {
        
fclose($file);
        
$status = ($stoptime - $starttime) * 1000;
        
$status = floor($status);
    }
    return 
$status;
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<body>
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="domain">
        Domain name:
        <table>
          <tr><td><input name="domainname" type="text" ></td></tr>
          <tr><td><input type="submit" name="submitBtn" value="Ping domain"></td></tr>
        </table>  
      </form>
<?php    
    
// Check whether the for was submitted
    
if (isset($_POST['submitBtn'])){
        
$domainbase = (isset($_POST['domainname'])) ? $_POST['domainname'] : '';
        
$domainbase = str_replace("http://","",strtolower($domainbase));
        
        echo 
'<table>';

        
$status = pingDomain($domainbase);
        if (
$status != -1) echo "<tr><td>http://$domainbase is ALIVE ($status ms)</td><tr>";
        else               echo 
"<tr><td>http://$domainbase is DOWN</td><tr>";

         echo 
'</table>';
    }
?>
</body>  Â