Papermashup

Subscribe


Tweets


"RT @Eva_Shaughnessy: #finedining @romomobilecafes restaurant this evening with #NewHair #winning #SaturdayNight #GirlsNightOut http://t.co/…"

@ashleyford 3 weeks ago

"RT @kycutwilson: @ashleyford @burgerbeartom incredible. There's 5 more left! Shout about it!!"

@ashleyford 4 weeks ago

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Papermashup

PHP check if your server is offline

Use PHP and CURL to check is your server is online

AshleyAshley

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>

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Comments 6
  • Sagive
    Posted on

    Sagive Sagive

    Reply Author

    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
    Posted on

    ckiller ckiller

    Reply Author

    Cooool, thx!


  • Devon Gilchrist
    Posted on

    Devon Gilchrist Devon Gilchrist

    Reply Author

    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
    Posted on

    Brian Brian

    Reply Author

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


  • Dev Guy
    Posted on

    Dev Guy Dev Guy

    Reply Author

    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?