Check website status using php and CURL library
Many times in our webmaster activity we need some kinda tool that can monitor the activity on our website(s). Me personally I would like to know how much my website is down. It’s extremely important , and really our websites shouldn’t be down much; not at all. Hosting companies are very numerous our days. Can always pick another one better than the existing, one that provides a better uptime.
There are lot’s of services over the WWW where you can pay to have your site monitored and be notified when this is down. The nasty thing here is that they get good money for a serivce that you can easily offer to yourself with a minimum effort.
So let ’s see what’s to be done!
In the article here I chosen to use the CURL PHP library instead of SOCKETS connections. Personally I don’t like sockets in php and I don’t see why we should use them when we don’t really need and when there is another nice way of doing things. CURL is really nifty, can do awesome things. But I am not gonna talk about that here, maybe in a future article
The whole idea here…
Is to have a PHP script(that can be eventually set as a CRON Linux CRON or scheduled task on WIN systems) that would try to open the site just like you do in your browser. If connection times-out than something is wrong obviously. Also, as stated into the standards, there is a HTTP response send back. For a complete reference of HTTP status codes see the HTTP protocol description article.
To accomplish that I chose to use the CURL PHP extension that me personally I like it very much since is a pretty flexible one, that can do so powerful things without too much trouble (I could mention here the easiness of getting the returned HTTP status of a connection that, as presented on other resources using sockets, would mean manually parse the response headers). Basicly, as stated in W3C standards, a successful connection is indicated by a 2XX HTTP code. So the presented script here will try to load the site and compare the returned status with 2XX.
function Visit($url)
{
$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, 5);
$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;
}
if(Visit("http://www.domain.com"))
echo "Website OK"."n";
/*if site down*/
else
echo "Website DOWN";
Hope you enjoy it and why not … find it useful ![]()

RSS/XML
April 6th, 2008 at 1:13 am
This script works like a charm! Also useful if you have a folder on a server using both http and https. This can test if a website can be visited via https first and then redirects if so. The php server variables are useless if the same folder uses both protocols because when you access the site via regular http it will let you and return nothing about https being on
July 20th, 2008 at 8:27 pm
i cant run it! it wrong or my server not support curl
September 23rd, 2008 at 2:24 am
Thanks alot!
November 2nd, 2008 at 6:40 pm
Works great! check it out - http://colerainebb.co.uk - sometimes the main site @boys-brigade.org.uk takes a break, if so the site is redirected to our @blogspot page!
Many thanks!
November 19th, 2008 at 4:55 am
hey there is one problem with this script..it gives you down status if domain is set redirection….looking for this ?
November 19th, 2008 at 4:59 am
like google.com