Check if Website is Available

Performs a cURL-Request to check, if a website exists / is online

Technique #1


       if (isDomainAvailible(''))
               echo "Up and running!";
               echo "Woops, nothing found there.";

       //returns true, if domain is availible, false if not
       function isDomainAvailible($domain)
               //check, if a valid url is provided
               if(!filter_var($domain, FILTER_VALIDATE_URL))
                       return false;

               //initialize curl
               $curlInit = curl_init($domain);

               //get answer
               $response = curl_exec($curlInit);


               if ($response) return true;

               return false;

View Demo

Technique #2

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);
       curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
       curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE);
       //echo curl_error($ch);
       $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
       if($httpcode>=200 && $httpcode<300) return true;
       else return false;
if (Visit(""))
       echo "Website OK"."n";
       echo "Website DOWN";

Technique #3

       if(strlen($r)>1) {
       echo("<span class='online'>Online</span>");
       else {
       echo("<span class='offline'>Offline</span>");


  1. NetHawk
    Permalink to comment#

    In technique #2 it should probably read
    ... && $httpcode<400) return true;

    instead of
    ... && $httpcode<300) return true;

    Otherwise a redirect would be regarded as “server down”.

    • Eduardo Suarez
      Permalink to comment#

      Hello NetHawk.
      I tested and you are totally right. $httpcode<300 always returns server down.
      Anyway, I made a little modification so I can test several sites, but sometimes, sites appear down randomly, what could be wrong if you can help me? I tried to do this for a cron job.

      =200 && $httpcode”.$sitio.””;
      echo “DOWN->”.$sitio.””;

    • Eduardo Suarez
      Permalink to comment#

      It seems like code was cutted… Anyway, the only modification I made, was put urls in an array, then test one by one in a foreach loop.

    • Downforeveryoneorjustme
      Permalink to comment#

      Here is an alternative for this method :

  2. Vimal Verma
    Permalink to comment#

    how to use it ?????????
    what is the method

    • NetHawk
      Permalink to comment#

      Copy code from example #2 into a text file. Give it the extension .php. Move the file to your webserver and call the url that points to the file. If you have a PHP enabled server (most are) the code will check if Google is up (probably always).

      You can take it from there, but you will need some basic PHP knowledge.

  3. Jason
    Permalink to comment#

    Which technique is better to use? advantages? disadvantages?

    • NetHawk
      Permalink to comment#

      #1 checks if the url is valid, that’s a great plus, if the urls are entered by a user and don’t come out of a database or from a list with urls known to be valid.

      #2 on the other hand submits a user agent string. Without it, some server will answer the request with a error 403 (forbidden) or 405 (method not allowed).

      Conclusion: I suggest #2 but with the filter test of #1.

      Actually, Chris should say something about this, because he is running, a service that probably uses this technique (see button in the footer of this page – great service, highly recommended).

    • Chris Coyier
      Permalink to comment#

      We actually use Ruby and some methods that are far more complex =). But the principal is the same.

  4. Rob
    Permalink to comment#

    Thanks! I’m wondering if uses the same method?

    Also, what is the purpose of the agent?

    • NetHawk
      Permalink to comment# is probably based on a similar mechanism, although it’s not clear what language is used.

      The UserAgent is a string sent by any browser to identify itself. At least, that was the intention. Nowadays many browser lie about the details. However, as long as a UserAgent is sent, the web server usually sees a request as legitimate. Also robots, e.g. Google have UserAgent strings to identify them.

      If no UserAgent string is sent, some web server don’t let you access the site for whatever reason (they probably don’t like the idea that some tool or other server accesses their pages directly).

      You can see the UserAgent string of your browser (among other information) on this page:

    • Willem

      They use php and I think code is similar to the example but with some changes, http is not needed in their form and it is needed in the example on this website however it’s easy to add a function that is able to strip or add http:// or other parts eg if you do not want to allow subdomains to be checked like is replacing parts of url’s.. has an index.php file so it is using php, 99% sure: I have scripts to display information about websites and to extract information send by their server…

      Thank you for sharing!

  5. jack
    Permalink to comment#

    none of these techniques really seem to work…everytime i check something like it always comes back as 200…does anyone know of a way to find out if a website is unavailable like *cough* *cough* godaddy *cough* *cough*

    • Dale Hurley
      Permalink to comment#

      It is a simple whois look up. You need to create a socket connection on port 43. It is just getting PHP to connect to the exact same whois server terminal uses when you do a whois lookup.

    • Dale Hurley
      Permalink to comment#

      It is a simple whois look up. You need to create a socket connection on port 43. It is just getting PHP to connect to the exact same whois server terminal uses when you do a whois lookup.

    • Dale Hurley
      Permalink to comment#

      Sorry for some reason it is not showing the code, you can find a good tut at which details how to build one.

  6. Luciano Mammino
    Permalink to comment#

    There’s another solution that uses sockets:

  7. Eddie Gear
    Permalink to comment#

    Hi Chris,

    Any idea, how can I check if a bunch of URLS are valid. For example. I need to run a check on 1000 twitter profiles and I need to know if they are active or invalid URLS. I want to be able to paste the entire list of twitter urls and check if they are valid pages.

  8. Aneek
    Permalink to comment#

    But is there any API from to check it? I think using web services will help the cause more effectively. Say as I did in getting the alexa ranking,

  9. AkhileshThakur
    Permalink to comment#

    Great concepts

  10. Alabastar
    Permalink to comment#

    Seems to me that these scripts wouldn’t actually work if there was a redirect. So “” and “” might return different results even if the site was up.


  11. Alabastar
    Permalink to comment#

    Anyone know how “” does it? That script seems to work every time, redirects or not…

  12. Kevin
    Permalink to comment#

    Those codes are not bulletproof. I tried different domains at and the script fails on some domains. is such an example.

    However it works at Not sure what method they are using…

  13. Christian
    Permalink to comment#


    Thanks for the tuts. But, one question though. How do you make sure that it is not your internet connection that has disconnected?

    Thanks a lot.

    • Riiich
      Permalink to comment#

      You wouldn’t have even been able to connect to the server to run the script if this was the case : )

  14. Mystic
    Permalink to comment#

    None of these scripts work for me!
    I am trying to get the script to tell me if my server is online. I have no domain, so I’m using the IP (and somtimes a port).
    When i have entered the IP to my server, it says it’s online.
    Also; when I am shutting down the server, it still says it’s online…
    How can I fix this? Any ideas?

    – Mystic

  15. Patrick
    Permalink to comment#

    That’s a good technique and a very similar technique is posted here : , i think this technique is similar yet effective.



  16. Alpeshkumar Godhani
    Permalink to comment#


    it’s very use full trick #2

    it’s solve my issue check 500 URL at one time

  17. Zen
    Permalink to comment#

    So hard :D Thanks

  18. Joseph
    Permalink to comment#

    I have this situation where the url redirects to another url which is dead. How can I check for this kind of errors?

  19. Maxol

    I started using technique 1 and some links were not reported correctly so I added technique 2 and the combination seems to be working fine:

                        if (!isDomainAvailible($Url) or (!Visit($Url)))
                            # Auw!!
                            $Status = 'Seems down...';
                        } else {
                            # Okay
                            $Status = 'Up!';
  20. grammar_police
    Permalink to comment#

    “Availible” is not a word.

    • Caroline
      Permalink to comment#

      It’s a word

    • Semantic_police
      Permalink to comment#

      Grammar Police, this is a false arrest! “Availible” is not a grammatical error, so you have no jurisdiction.

      Typo Police, at ease; Spelling Police, arrest that offender, pronto!

  21. Zahid
    Permalink to comment#

    Given script are very useful, I was looking such script to check site is live or not, But few more things if you can add, Some sites are up but parked, any way to check that, sites are properly hosted on server not parked

  22. Hans Muster
    Permalink to comment#

    hmm.. none of the methods above work for me ? aka
    1. and 2. method return always true and 3rd throws errors like a baws

  23. Hans Muster
    Permalink to comment#

    EDIT: 2. Method works but it’s inverted for me (ture -> DOWN, false -> UP) … don’t ask me why

  24. syscabm
    Permalink to comment#

    Hi guys..
    I thonk, just use this one..

  25. Curtis
    Permalink to comment#

    I see a lot of people having problems here.

    Be sure that if you use example two, that you see NetHawk’s comment about making 200<=$httpcode<400.

    Also, it’s good practice to declare functions with the first letter being lowercase.

    Much love for CSS Tricks over here.


  26. Dhivyaa
    Permalink to comment#

    See I have a form which has website to be taken. We have to check if the website is valid or not. Where should I embed the codes? That is, in the #1, the url has to be manually entered. But I need it to be done automatically. How am is supposed to do it ?

  27. Nanda
    Permalink to comment#

    I tested and you are totally right.but i had doubt. this scripts used for one domain. but, i need to know 50 domain is working or not at a ti me.
    pls reply

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences like this:

  function example() {
    element.innerHTML = "<div>code</div>";

We have a pretty good* newsletter.