Search code examples
phpweb

Get the site status - up or down


<?php
$host = 'http://google.com';
if($socket =@ fsockopen($host, 80, $errno, $errstr, 30)) {
echo 'online!';
fclose($socket);
} else {
echo 'offline.';
?>

I'm using the above program to get the status of site. But I always get an offline message. Is there any mistake with the code?


Solution

  • The hostname does not contain http://, that is only the scheme for an URI.

    Remove it and try this:

    <?php
    $host = 'google.com';
    if($socket =@ fsockopen($host, 80, $errno, $errstr, 30)) {
    echo 'online!';
    fclose($socket);
    } else {
    echo 'offline.';
    }
    ?>