Code for ping check an ip and report back?

Associate
Joined
6 Jan 2008
Posts
316
Hi there,

I'm looking for away to create a fail on my webpage which report the status of other webpages/device on the internet.

So for example:
say if I want to know if google.com is working without going to the site; I would ping the address and if the ping replies ok, then I get a green icon. And if its doesn't respond I get a red icon.

Is there anyway to write a code to do this?

Thanks.
 
Because not every web host responds to ping you could instead just use fsockopen in php to open a connect to a remote host. You can also detect the header response code from the server this way (because there are many instances where a server might be responding to ping but giving a 500 error code or similar, for instance)

So it would show green for a 200/301/302 header response but red for everything else.

Edit: just realised you'll probably want fsockopen
 
Last edited:
Hi
Thank you very much for your replies on this.

I can see how the solutions you've provided work for other webhosts, but what happens if its a device, say like a router? What would be the best solution on that?
 
HI
I've trying to use fsockopen - I've found this guide here:
http://www.developertutorials.com/t...nning-and-service-status-checking-in-php-870/

Code:
<?php
function check_port($port) {
    $conn = @fsockopen("www.google.com", $port, $errno, $errstr, 0.2);
    if ($conn) {
        fclose($conn);
        return true;
    }
}

function server_report() {
    $report = array();
    $svcs = array('21'=>'FTP',
                  '22'=>'SSH',
                  '25'=>'SMTP',
                  '80'=>'HTTP',
                  '110'=>'POP3',
                  '143'=>'IMAP',
                  '3306'=>'MySQL');
    foreach ($svcs as $port=>$service) {
        $report[$service] = check_port($port);
    }
    return $report;
}

$report = server_report();
?>
<table>
    <tr>
        <td>Service</td>
        <td>Status</td>
    </tr>
    <tr>
        <td>FTP</td>
        <td><?php echo $report['FTP'] ? "Online" : "Offline"; ?></td>
    </tr>
    <tr>
        <td>SSH</td>
        <td><?php echo $report['SSH'] ? "Online" : "Offline"; ?></td>
    </tr>
    <tr>
        <td>SMTP</td>
        <td><?php echo $report['SMTP'] ? "Online" : "Offline"; ?></td>
    </tr>
    <tr>
        <td>HTTP</td>
        <td><?php echo $report['HTTP'] ? "Online" : "Offline"; ?></td>
    </tr>
    <tr>
        <td>POP3</td>
        <td><?php echo $report['POP3'] ? "Online" : "Offline"; ?></td>
    </tr>
    <tr>
        <td>IMAP</td>
        <td><?php echo $report['IMAP'] ? "Online" : "Offline"; ?></td>
    </tr>
    <tr>
        <td>MySQL</td>
        <td><?php echo $report['MySQL'] ? "Online" : "Offline"; ?></td>
    </tr>
</table>

But when I try that (using google as the domain) I'm just getting a blank page.
So I also tried another script: http://www.vbteam.info/how-to/30679-how-to-code-a-status-checker-for-a-port-and-ip.html

Code:
<?php	
     /*@author     : WhipNRange*/
     /*@title      : Server Status Checker*/
     /*@description: Checks the server status of any hostname, eg. RuneScape Private Server using customizable images! Great for hiding your server IP, and still showing your server status whatever way you like!*/
	
     $status = GetServerStatus('finalcalibur.no-ip.org',43594); // IP or Hostname Goes Here
     function GetServerStatus($site, $port) {
          $fp = @fsockopen($site, $port, $errno, $errstr, 2);
          if (!$fp) { //OFFLINE Status
               $imagepath="http://i46.tinypic.com/fdehbc.jpg"; //<------ (EDIT THIS) OFFLINE IMAGE LINK!
               $image=imagecreatefrompng($imagepath);
               header('Content-Type: image/png');
               imagepng($image);
          } else { //ONLINE Status
               $imagepath="http://i48.tinypic.com/mmvpsm.jpg"; //<------ (EDIT THIS) ONLINE IMAGE LINK!
               $image=imagecreatefrompng($imagepath);
               header('Content-Type: image/png');
               imagepng($image);
          }
     /*Enjoy! Hope this helped. :D*/
     }
?>

And same issue blank page. But If i use simple code like:
Code:
<?php
$connection = @fsockopen("www.google.com", 80);
if ($connection) {
    echo "Port 80 on www.google.com is open.";
    fclose($connection);
} else {
    echo "Port 80 on www.google.com is not open.";
}

This works correctly. Any idea why this is happening?
 
The server is probably discarding your request because it thinks it's a bot. Use the curl extension for PHP, and don't make lots of requests to the server over a short timeframe.
 
Hi
Ok I've shortened the code so its now and it works fine;

Code:
<?php	
if(@fsockopen("www.google.com", "80", $errno, $errstr, TIMEOUT)){
    echo "<img src='/custom/online.png' />";
}else{
    echo "<img src='/custom/offline.png' />";
}

But I'm now faced with another issues; My webpage is using Joomla to run. So when I run this script, it comes up with a green or red dot on a blank page. But what I need to do is get this to run within a table on a Joomla article.

At the moment the html code below; displays the 'call php' area which I need to run; could you help me code it so that it runs on page load?

Thank you.
Code:
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="PageCreate">
<title>New Page</title>
</head><body><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px"></p><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px"><br></p>
<table cellspacing="0" cellpadding="0" style="border-collapse: collapse">
<tbody>
<tr>
<td valign="middle" style="border-style: solid; border-width: 1.0px 1.0px 1.0px 1.0px; border-color: #cbcbcb #cbcbcb #cbcbcb #cbcbcb; padding: 0.0px 5.0px 0.0px 5.0px">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; text-align: center; font: 12.0px Helvetica"><b>Severs</b></p>
</td>
<td valign="middle" style="border-style: solid; border-width: 1.0px 1.0px 1.0px 1.0px; border-color: #cbcbcb #cbcbcb #cbcbcb #cbcbcb; padding: 0.0px 5.0px 0.0px 5.0px">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; text-align: center; font: 12.0px Helvetica"><b>Status</b></p>
</td>
</tr>
<tr>
<td valign="middle" style="border-style: solid; border-width: 1.0px 1.0px 1.0px 1.0px; border-color: #cbcbcb #cbcbcb #cbcbcb #cbcbcb; padding: 0.0px 5.0px 0.0px 5.0px">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">Google</p>
</td>
<td valign="middle" style="border-style: solid; border-width: 1.0px 1.0px 1.0px 1.0px; border-color: #cbcbcb #cbcbcb #cbcbcb #cbcbcb; padding: 0.0px 5.0px 0.0px 5.0px">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px"><span class="Apple-style-span" style="border-collapse: separate; font-family: verdana, arial, sans-serif; font-size: 11px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">&lt;!--#include virtual="/custom/status.php"--&gt;<br></span></p>
</td>
</tr>
<tr>
<td valign="middle" style="border-style: solid; border-width: 1.0px 1.0px 1.0px 1.0px; border-color: #cbcbcb #cbcbcb #cbcbcb #cbcbcb; padding: 0.0px 5.0px 0.0px 5.0px">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">My router</p>
</td>
<td valign="middle" style="border-style: solid; border-width: 1.0px 1.0px 1.0px 1.0px; border-color: #cbcbcb #cbcbcb #cbcbcb #cbcbcb; padding: 0.0px 5.0px 0.0px 5.0px">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px"><span class="Apple-style-span" style="border-collapse: separate; font-family: verdana, arial, sans-serif; font-size: 11px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">&lt;!--#include virtual="/custom/status.php"--&gt;</span></p>
</td>
</tr>
<tr>
<td valign="middle" style="border-style: solid; border-width: 1.0px 1.0px 1.0px 1.0px; border-color: #cbcbcb #cbcbcb #cbcbcb #cbcbcb; padding: 0.0px 5.0px 0.0px 5.0px">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">My Computer</p>
</td>
<td valign="middle" style="border-style: solid; border-width: 1.0px 1.0px 1.0px 1.0px; border-color: #cbcbcb #cbcbcb #cbcbcb #cbcbcb; padding: 0.0px 5.0px 0.0px 5.0px">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px"><span class="Apple-style-span" style="border-collapse: separate; font-family: verdana, arial, sans-serif; font-size: 11px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">&lt;!--#include virtual="/custom/status.php"--&gt;</span></p>
</td>
</tr>
<tr>
<td valign="middle" style="border-style: solid; border-width: 1.0px 1.0px 1.0px 1.0px; border-color: #cbcbcb #cbcbcb #cbcbcb #cbcbcb; padding: 0.0px 5.0px 0.0px 5.0px">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica">My webpage host</p>
</td>
<td valign="middle" style="border-style: solid; border-width: 1.0px 1.0px 1.0px 1.0px; border-color: #cbcbcb #cbcbcb #cbcbcb #cbcbcb; padding: 0.0px 5.0px 0.0px 5.0px">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px"><span class="Apple-style-span" style="border-collapse: separate; font-family: verdana, arial, sans-serif; font-size: 11px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">&lt;!--#include virtual="/custom/status.php"--&gt;</span></p>
</td>
</tr>
</tbody>
</table>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px"><br></p><p></p>

</body></html>
 
This is some code I was using on another project

Code:
function getHeaders($domain)
{
        $server = str_replace("http://","",$domain);

        $urlafterslash = strstr($server,"/");

        $server = str_replace($urlafterslash,"",$server);

        $portno = 80;
        $method = "HEAD";

        if ($urlafterslash == "" || $urlafterslash == 'null')
        {
        $urlafterslash = "/";
        }

        $http_response = "";
        $http_request .= $method." ".$urlafterslash ." HTTP/1.0\r\n";
        $http_request .= "\r\n";
        $http_request .= "Content-type: application/x-www-form-urlencoded\r\n";
        $http_request .= "Cache-Control: no-cache\r\n";
        $http_request .= "User-Agent: MSIE\r\n";
        $http_request .= "Connection: close\r\n";

        $fp = @fsockopen($server, $portno, $errno, $errstr);

        if($fp)
                {
                fputs($fp, $http_request);
                while (!feof($fp)) $http_response .= fgets($fp, 128);
                fclose($fp);
                }
return $http_response;

}
 
Back
Top Bottom