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;
}
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):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.