Home > Article > Backend Development > PHP implementation code to detect server status
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" ; } ?> |