Check website status using php and CURL library

September 16, 2007

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);


//echo curl_error($ch);

$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);


if($httpcode>=200 && $httpcode<300) return true;

else return false;



           echo "Website OK"."n";

      /*if site down*/


          echo "Website DOWN";

Hope you enjoy it and why not … find it useful ;)


46 Comments for “Check website status using php and CURL library”

  1. person Says:

    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

  2. nguyenxuannghia Says:

    i cant run it! it wrong or my server not support curl

  3. QLH máy tính Says:

    Thanks alot!

  4. Keith Greer Says:

    Works great! check it out - - sometimes the main site takes a break, if so the site is redirected to our @blogspot page!

    Many thanks!

  5. yogesh Says:

    hey there is one problem with this gives you down status if domain is set redirection….looking for this ? :(

  6. yogesh Says:


  7. peace pal Says:

    Curl is definitely better than socket, but i dont know how to enable curl in remotely hosted server.

  8. claude Says:

    I don’t think there’s a way to enable it without the hosting company help…CURL compiles with PHP so the hositng guys have to do it


  9. Ashish saxena(chandigarh) Says:

    hi sir
    above code is not properly working.when site is down ,how can get red of this error/warning..plz help me

    try this site:
    2:30 PM Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in /home/brains1/public_html/check.php on line 7

    Warning: fsockopen() [function.fsockopen]: unable to connect to (php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution) in /home/brains1/public_html/check.php on line 7
    Website DOWN Down!

  10. claude Says:

    Try turnning of error reporting ( error_reporting(0) ) or put an @ sign in front of function call.



  11. vignesh Says:

    Sir When i run the above code its showing the following error.

    Fatal error: Call to undefined function curl_init() in C:\projects\site_availability_status\status.php on line 18

  12. claude Says:

    it’s because you don’t have curl enabled in your php.ini.

    Contact your host and they will do that for you

  13. Mudit Grover Says:

    To check status of website which is redirected


    if($httpcode>=200 && $httpcode

  14. Jason Says:

    can this code test for https sites?
    as i tried it and it gives me this
    “SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed”

    any way to get this working?

  15. Regina Says:


    To get around the SSL certificate stuff, what I do is add these lines:

    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE);

  16. DEViANCE Says:

    Works great, i have used the code in a ddl auto submitter script to see if the sites are online even before submission. Thanks :)

  17. tester Says:

    Hey this code is good i tried with https but it is showing the website is down every time(say why it is showing like that

  18. Peder Johnsen Says:

    Any idea why this only works on websites hosted on the same server? When trying websites on other servers it just puts them as “website down”

  19. claude Says:


    it’s probalby a certificate problem. you can disable it byu using

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    or the hard way, poingint to right location

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_CAINFO, getcwd() . “/path/to/crt”);

  20. The master Says:

    @Peder Johnsen

    since it works on locl website, I would guess there’s a DNS problem on that server. IE. curl cannot get dns for a certain domain name. but you need to debug to be sure


  21. Peder Johnsen Says:

    @The master

    Ok! :) Well, I put the script on the server I was gonna have it in the first place, and it works there so! :).

  22. glycerine Says:

    I tried to run the script but all the results i am getting is “WEBSITE DOWN” although when i visit the site/domain, the site is up. How come? is there something i am missing here?

  23. sanjiwani Says:

    Thanks, your code helpe me a lot

  24. Matt Says:

    hi i get the error name lookup timed out

    and it always says website DOWN
    what todo?

  25. claude Says:

    @matt maybe curl extension is not enabled or perhaps the website you are testing is reponding slower than how many seconds are set into curl_setopt($ch, CURLOPT_TIMEOUT, 5);

    hope this helps

  26. matt Says:

    so i need to lower that number?
    curl is enabled because it has worked in the past :D on the same site
    but since a few days it stoped working

  27. claude Says:

    5 seconds timeout should be fine.

    it hard to tell what’s wrong..
    maybe try to echo the $httpdcode var inide code and see what that is…

  28. matt Says:

    i did and it said time out…
    the script stoped working a few days ago

  29. claude Says:


    try running the script from on another server…it might be possible that on hte server you are currently running it, there are some sort of DNS issues for eg and curl cannot lookup your domain name.
    I might be wrong but it’s a possibility :)

  30. matt Says:

    it worked when i tryed it on my localhost with curl lib on
    what i need to do to fix this problem on the online server?

  31. claude Says:

    @matt first you need to see what the problem is :)

    see if curl gives any notices, try verbosing the output etc..

    hope that helps

  32. MAtt Says:

    the following script works perfect ( it doesnt show the images but that is normal i supose?)
    on the server i want to run the script..
    so what else can it be if Curl is working fine?

    curl information

    cURL support enabled
    cURL Information libcurl/7.20.0 OpenSSL/0.9.8b zlib/1.2.3

  33. claude Says:

    try doing a print_r(curl_getinfo($ch)); inside the script. if it takes kinda long to return and you get an array with almost all values 0 than there’s more likely a DNS/network issue on that server.


  34. Giuly Says:

    Helo can i use this script to redirect to another website mirror if main site is down? Ie If website A don’t respond after 10 second then is down so script redirect to website mirror b hosted on other host
    Can you post script because i’m a newbie and don’t know ho to make it

  35. dinesh kumar Says:

    curl is installed in my server..but when i execute the code, it is showing as URL not found on this server.Same code is working from my local server.
    I tried with print_r(curl_getinfo($ch)); and it returned as array with almost all values 0.
    Could someone help me on this.

  36. لینکدونی پائیز Says:

    Another easy working way:

  37. لینکدونی پائیز Says:

    Another easy working way:

    if (p___url_exist(’’))
    { print(’url exists.’);
    } else
    { print(’url doesn\’t exist.’);

    function p___url_exist($url)
    { $r = $h = null;
    $h = curl_init($url);
    if (false === $h) { return (false); };
    curl_setopt($h, CURLOPT_HEADER, false);
    curl_setopt($h, CURLOPT_FAILONERROR, true);
    curl_setopt($h, CURLOPT_NOBODY, true);
    curl_setopt($h, CURLOPT_RETURNTRANSFER, false);
    $r = (curl_exec($h) ? true : false);
    return ($r);

  38. Matt Farmer Says:

    If you don’t want to download the entire file, its important to set the following flag:

    curl_setopt($ch, CURLOPT_NOBODY, true);

    Doing so will only fetch the Header information, but not the actual content. This will significantly improve performance / reduce used bandwidth if you are checking files that are large (such a images / videos)

  39. steve Says:

    Useful scripts, Thanks.

  40. bobby Says:

    great script, thank you.

  41. Raja Says:

    Useful script, great job and thanks for sharing….

  42. Steve Lawson Says:

    Works great!

    I made a slight change, so it returns the HTTP Code if the access fails:

    * Use this function to do a “test visit” of a website. It returns true if the
    * site was accessed without error or timeout. Otherwise it returns the HTTP
    * error code.
    * @param string $url The URL of the site to test visit.
    * @return mixed true if able to visit else the http code is returned. Be sure to use ‘=== true’ to test for success.
    function visit($url)
    $agent = “Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)”;
    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);
    //echo curl_error($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if($httpcode>=200 && $httpcode

  43. Steve Lawson Says:

    Here’s the last bit (see above comment):

    else return $httpcode;

  44. Steve Lawson Says:

    Use it like this (see above two comments):

    $http_code = Visit(””);

    if ($http_code === true)
    echo “Website OK”;
    echo “Website DOWN with $http_code error!”;

  45. Partha Says:

    I am using then same with some modification. But it is not working with following link:

    It is giving 404 as response code.


  46. Benaya Paul Says:

    Nice script, thanks for sharing…

Post a comment

Name (required)

Mail (will not be published) (required)


To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
Click to hear an audio file of the anti-spam word