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

               //get response
               $response = curl_exec($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
	   $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/>

<li><a href="?url="></a></li>
<li><a href="?url="></a></li>
<li><a href="?url="></a></li>

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


More tutorials from Papermashup
  • 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!

  • 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

  • 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?