本节分享的这段php代码,可以根据域名,采用fsockopen的方法来检测服务器的当前状态。
代码:
<? /** * 检测服务器状态 */ # Domain Name $domainName = "http://www.jb200.com" ; #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" ; } ?>