Home >> Snippet List >> Snippet

Name
Checking Server Status with PHP
Description
A small bit of code to check server status and report the results Easily combined with an emailer to send a text message to a phone for real-time alerting of server issues
PHP Snippet
<?PHP
 
// Domain name to check
$domainName = "http://www.psoug.org" ;
 
FUNCTION DomainCheck($domain_name){
    $start_time = MICROTIME(TRUE);
    $openDomain = FSOCKOPEN ($domain_name, 80, $errno, $errstr, 10);
    $end_time  = MICROTIME(TRUE);
    $server_status    = 0;
 
    IF (!$openDomain){
		$server_status = -1;  
    }ELSE{
        FCLOSE($openDomain);
        $status = ($end_time - $start_time) * 1000;
        $server_status = FLOOR($server_status);
    }
    RETURN $server_status;
}
 
$server_status = DomainCheck($domain_name);
 
 
IF ($server_status != -1) {
	PRINT "Cannot reach the server." ;
}ELSE{
	PRINT "Server is responding properly." ;
} 
 
?>