php get_headers a good way to tell if a site is up?

4.1k Views Asked by At

I'm still fairly new to php, can you comment on whether the code below is any good to tell if a site is up or down and if it might not be suitable the reasons why and better altneratives?

Thanks in advance.

$siteHeader = @get_headers($url , 1);
if ($siteHeader > 1) {
    $siteUp = true;
} else {
    $siteUp = false;
}
3

There are 3 best solutions below

1
On

Update: The more I think of it, the less fine it looks. I 've expanded on my initial answer, which in retrospect was naive.

It's fine for basic usage, but you might want to check the HTTP response code as well instead of just checking if you got a response. The way the code is right now, it just tells you that there was someone listening on the other side, which is a long way from what most people would consider "the site is up".

Here's how to easily isolate the HTTP response code (or get false if the request failed):

$headers = get_headers('http://www.google.com');
$code = $headers ? intval(end(explode(' ', $headers[0], 2))) : false;

Apart from that, there's also the matter of redirects: what do you do if you see a redirect? The server you queried might be OK, but the server you are redirected to might be down. If someone typed the URL in a browser they 'd be redirected and ultimately time out, while the one-step test would say everything's OK. What if there's a redirect loop? A browser would detect this and eventually time out, but you need to write quite a lot of code to do the same.

So in the end cURL does look like the only surefire solution because it does all this transparently.

2
On

Depending on your case but, specially if your URL is submitted by an user, I would go for something like that.

//returns true, if domain is availible, false if not
function isDomainAvailible($domain)
{
   //check if URL is valid
   if(!filter_var($domain, FILTER_VALIDATE_URL)){
           return false;
   }

   $agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
   $ch = curl_init();
   curl_setopt ($ch, CURLOPT_URL, $url);
   curl_setopt ($ch, CURLOPT_USERAGENT, $agent);
   curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt ($ch,CURLOPT_VERBOSE, FALSE);
   curl_setopt ($ch, CURLOPT_TIMEOUT, 10);
   curl_setopt ($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
   curl_setopt ($ch,CURLOPT_SSLVERSION, 3);
   curl_setopt ($ch,CURLOPT_SSL_VERIFYHOST, FALSE);
   $page=curl_exec($ch);
   //echo curl_error($ch);
   $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
   curl_close($ch);
   if ($httpcode >= 200 && $httpcode < 300) 
    return true;
   else 
    return false;
}

Mostly because some servers won't respond if you don't send an useragent.

0
On

i use curl, but thats just me:

function check($url, $ignore = '')
{
    $agent = "Mozilla/4.0 (B*U*S)";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);

    curl_setopt($ch, CURLOPT_VERBOSE, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 45);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 45);

    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 5); //follow up to 10 redirections - avoids loops


    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //fix for certificate issue
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //fix for certificate issue


    $page = curl_exec($ch);
    $err = curl_error($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    $codes = array(
        0 => 'Domain Not Found',
        100 => 'Continue',
        101 => 'Switching Protocols',
        200 => 'OK',
        201 => 'Created',
        202 => 'Accepted',
        203 => 'Non-Authoritative Information',
        204 => 'No Content',
        205 => 'Reset Content',
        206 => 'Partial Content',
        300 => 'Multiple Choices',
        301 => 'Moved Permanently',
        302 => 'Found',
        303 => 'See Other',
        304 => 'Not Modified',
        305 => 'Use Proxy',
        307 => 'Temporary Redirect',
        400 => 'Bad Request',
        401 => 'Unauthorized',
        402 => 'Payment Required',
        403 => 'Forbidden',
        404 => 'Not Found',
        405 => 'Method Not Allowed',
        406 => 'Not Acceptable',
        407 => 'Proxy Authentication Required',
        408 => 'Request Timeout',
        409 => 'Conflict',
        410 => 'Gone',
        411 => 'Length Required',
        412 => 'Precondition Failed',
        413 => 'Request Entity Too Large',
        414 => 'Request-URI Too Long',
        415 => 'Unsupported Media Type',
        416 => 'Requested Range Not Satisfiable',
        417 => 'Expectation Failed',
        500 => 'Internal Server Error',
        501 => 'Not Implemented',
        502 => 'Bad Gateway',
        503 => 'Service Unavailable',
        504 => 'Gateway Timeout',
        505 => 'HTTP Version Not Supported'
    );

    $httpcode_out = 'http: ' . $httpcode . ' (' . $codes[$httpcode] . ')';
    $err = 'curl error: ' . $err;

    $out = array(
        $url,
        $httpcode_out,
        $err
    );

    if ($httpcode >= 200 && $httpcode < 307)
    {//good
        return array(
            'Work',
            $out
        );
    }
    else
    {//BAD
        return array(
            'Fail',
            $out
        );
    }
}