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.
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>';
}
<?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>
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.
Pretty cool. Reminds me of http://www.downforeveryoneorjustme.com.
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?
Designer and web developer, Co-founder and Technical Director at Harkable.com London. Previously I worked at InMobi, Spotify and MySpace. Interests include photography and making short videos. Also an avid F1 fan.
Follow us on Twitter and get in-stream updates.
Subscribe to all the Papermashup tutorials and articles straight to your RSS reader.
Sign up and get all the Papermashup tutorials straight to your inbox.
4 discussions around PHP check if your website is up