PHP fgets() fails to get from domains on same IP

316 views Asked by At

I am trying to perform basic uptime monitoring for all my websites hosted under the same shared hosting account. I want to isolate network issues from local (server load) issues, so I cant use an external uptime monitor.

The following php script can poll sites hosted on other IP addresses, but not it's own. I can wget & curl from my primary and add-on domains (all same IP) via an SSH console, but I cant seem to do the same loopback operation via PHP. Why?

<?php
check('some-other-host.com', 'validation text 2'); //succeeds
check('addon-domain-on-same-host.com', 'validation text 1'); //fails
// other sites go here //

function check($host, $find) {
    $fp = fsockopen($host, 80, $errno, $errstr, 10);
    if (!$fp) {
        echo "$errstr ($errno)\n";
    } else {
        $header = "GET / HTTP/1.1\r\n";
        $header .= "Host: $host\r\n";
        $header .= "Connection: close\r\n\r\n";
        fputs($fp, $header);
        while (!feof($fp)) {
            $str .= fgets($fp, 1024);
        }
        fclose($fp);
        if (strpos($str, $find) == false) {
            echo $host." is down. ";
            mail('[email protected]', $host.' is down.', $str);
        }
    }
}
?>
0

There are 0 answers