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
  • http://www.sagive.co.il Sagive

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

  • ckiller

    Cooool, thx!

  • http://www.devongilchrist.com Devon Gilchrist

    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.

  • Brian

    Pretty cool. Reminds me of http://www.downforeveryoneorjustme.com.

  • http://www.vivitech.co.uk/ Dev Guy

    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?