Friday, March 19, 2010

How to check the status of server/host using PHP ?

Recently I came across this trivial problem of checking whether a server is ONLINE or OFFLINE. I tried "Ping" command but the following approach was more elegant.



$host = "127.0.0.1"; // could be any host address 
$port = "80"; //  port you wanna check 

$fp = @fsockopen($host, $port, $errno, $errstr, 2);

if (!$fp) {
echo "DEAD";


else { 
echo "ALIVE";
}


?>

Have fun !