Posted By

goo on 01/14/13


Tagged

domain avalaible


Versions (?)

[PHP] Is Domain Avalaible


 / Published in: PHP
 

URL: http://css-tricks.com/snippets/php/check-if-website-is-available/

Want to know if a specific website is available? cURL is here to help. This script can be used with a cron job to monitor your websites.

  1. <?php
  2.  
  3. if (isDomainAvailible('http://www.css-tricks.com'))
  4. {
  5. echo "Up and running!";
  6. }
  7. else
  8. {
  9. echo "Woops, nothing found there.";
  10. }
  11.  
  12. //returns true, if domain is availible, false if not
  13. function isDomainAvailible($domain)
  14. {
  15. //check, if a valid url is provided
  16. if(!filter_var($domain, FILTER_VALIDATE_URL))
  17. {
  18. return false;
  19. }
  20.  
  21. //initialize curl
  22. $curlInit = curl_init($domain);
  23. curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,10);
  24. curl_setopt($curlInit,CURLOPT_HEADER,true);
  25. curl_setopt($curlInit,CURLOPT_NOBODY,true);
  26. curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true);
  27.  
  28. //get answer
  29. $response = curl_exec($curlInit);
  30.  
  31. curl_close($curlInit);
  32.  
  33. if ($response) return true;
  34.  
  35. return false;
  36. }
  37. ?>

Report this snippet  

You need to login to post a comment.