Home  >  Article  >  Backend Development  >  PHP implementation code to detect server status

PHP implementation code to detect server status

WBOY
WBOYOriginal
2016-07-25 08:56:561370browse
Sometimes it is necessary to detect the running status of the server in real time. This article shares a piece of PHP code to use a script to detect the status of the server. Friends in need can refer to it.

The PHP code shared in this section can use the fsockopen method to detect the current status of the server based on the domain name.

Code:

<?
/**
* 检测服务器状态
*/
# Domain Name
$domainName = "http://bbs.it-home.org" ;

#My Function
function DomainCheck($domainName){
    $startTime = microtime(true);
    $openDomain = fsockopen ($domainName, 80, $errno, $errstr, 10);
    $finishTime  = microtime(true);
    $serverStatus    = 0;

    if (!$openDomain) $serverStatus = -1;  
    else {
        fclose($openDomain);
        $status = ($finishTime - $startTime) * 1000;
        $serverStatus = floor($serverStatus);
    }
    return $serverStatus;
}

$serverStatus = DomainCheck($domainName);

# Results...
if ($serverStatus != -1) {
echo "Server is seemed off" ;
} else {
echo "Server is running well" ;
} 
?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn