Use your left/right keys to browse tutorials
PHP check if your website is up

PHP check if your website is up

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on January 30, 2012

Here’s a basic but useful snippet to either check that a website is online or domain exist. It’s a very simple function that uses CURL to check the response from a given URL. if a response is received we can assume that the site is up/online.

The Code


       function isSiteAvailable($url)
       {
               //check, if a valid url is provided
               if(!filter_var($url, FILTER_VALIDATE_URL))
               {
                       return 'URL provided wasn\'t valid';
               }

               //make the connection with curl
               $cl = curl_init($url);
               curl_setopt($cl,CURLOPT_CONNECTTIMEOUT,10);
               curl_setopt($cl,CURLOPT_HEADER,true);
               curl_setopt($cl,CURLOPT_NOBODY,true);
               curl_setopt($cl,CURLOPT_RETURNTRANSFER,true);

               //get response
               $response = curl_exec($cl);

               curl_close($cl);

               if ($response) return 'Site seems to be up and running!';

               return 'Oops nothing found, the site is either offline or the domain doesn\'t exist';
       }


	// check if site exists / is up
	if($_GET['url']){
	
	   $response = isSiteAvailable($_GET['url']);
	   $message = '<div class="response">'.$response.'</div>';
	}

Display the response


<?php echo $message;?>

<form action="" method="get">

Enter a URL below or click an example below:<br/>

<ul>
<li><a href="?url=http://harkable.com">harkable.com</a></li>
<li><a href="?url=http://google.com">google.com</a></li>
<li><a href="?url=http://sfdhjsdkhfskjfhskjfh.om">sfdhjsdkhfskjfhskjfh.om</a></li>
</ul>

<input name="url" type="text" value="<?php echo $_GET['url'];?>" />

</form>



More tutorials from Papermashup
Comments
6 discussions around PHP check if your website is up
  1. Sagive says:

    Most servers would send out a 404 page so why not check the header if already using curl… this more “check if your server if offline” rather then is your site online ;)

  2. ckiller says:

    Cooool, thx!

  3. Handy, thanks :) Will be great to set this to run a quick check on all the sites I maintain–especially for those that are sitting on the same server. Will be a nice layer of troubleshooting when needed.

  4. Dev Guy says:

    This is quite good. I want to create custom 404 pages, would I be able to use this to detect page was not found upon loading?





Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.
Subscribe
OR

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.
Subscribe

Get in contact

Please use the form below to get in contact. If your question is related to a free script download, please use the comments on the article page as community members are more likely to respond quicker than I can personally.

About Me

I'm Ashley Ford, Co-founder and Technical Director at Harkable.com London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.



What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on Papermashup.com please get in touch via the contact link below for advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.