Server Status Checker (for your website)

5 replies [Last post]
Posts: 1030

Here's the code:

<?php
setlocale(LC_ALL,"nl_BE");
mysql_connect("YOURDBHOST", "USERNAME", "PASSWORD");     
mysql_select_db("DATABASENAME") or die("Error opening database");        
 
 
 
$query = "SELECT `when` FROM world_stats_network ORDER BY id DESC LIMIT 1";
$result = mysql_db_query("DATABASENAME", $query) or die("There is something seriously wrong now!");
 
$row = mysql_fetch_assoc($result);
 
$last_try = convert_datetime($row['when']);
$now = time();
if(($now - $last_try) > 60)
{
    if(($now - $last_try) > 300)
    {
        echo '<span style="color:red;">Server is offline</span>';
    }
    else
    {
        echo '<span style="color:orange;">High server load detected</span>';
    }
}
else
{
    echo '<span style="color:green;">Server is online</span>';
}
 
function convert_datetime($str) 
{
    list($date, $time) = explode(' ', $str);
    list($year, $month, $day) = explode('-', $date);
    list($hour, $minute, $second) = explode(':', $time);
 
    $timestamp = mktime($hour, $minute, $second, $month, $day, $year);
 
    return $timestamp;
}
 
?>

What it does is it checks the timestamp in the database which the server updates every minute against the local time. If the difference between them is more than 5 minutes, the server must be down.

Posts: 180

Shouldn't it be:

(removed: fixed original post --Spodi)

PHP doesn't allow html tags that way.

Posts: 1691

I think that is what he had originally, but missed the last " on "PASSWORD", which corrupted the rest of his code.

Posts: 180

Why would it add <p> </p> because of the "?

I was saying the <p> </p> don't belong.

Posts: 1691

I think that is an issue with the parser, too. If you quote his original text, they're not there. So it must've been added due to line breaks.

Edit: Should be fixed now. Hopefully I didn't break anything else in the process... I edited the first two posts accordingly. Hope you guys don't mind.

Posts: 1030

Not at all, it looks readable now Wink