How to check Server status (up/down) using php

Post Reply
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

How to check Server status (up/down) using php

Post by Neo » Mon Jan 18, 2010 12:49 pm

This is a very good service up/down check code I found from net.
Note that you need to put up.png and down.png and use cs function to get information on specific service running on IP/Port.

Code: Select all

<?php
function cs($hostip, $port)
{
        if (!$ifcs = @fsockopen($hostip, $port, $errno, $errstr, 1)) // attempt to connect
        {
            echo '<img src="/up.png" alt="Up" />';
        }
        else
        {
            echo '<img src="/down.png" alt="Down" />';
            if ($ifcs)
            {
                @fclose($ifcs); //close connection
            }
        }
}

$u = shell_exec('uptime');
?>

<h2>Server uptime</h2> <?php echo $u; ?><br />
<h2>Current status</h2>
HTTP: <?php cs('192.168.0.1','80'); ?><br />
Post Reply

Return to “PHP & MySQL”