Here you find a PHP script to check if a website is online availability with PHP/cURL. The following PHP function checks if your website is online available or not. Website uptime and availability is important and you want your website to be always online available. When your website is down, you want to be notified about the downtime.

PHP & cURL script to check your website’s online status & availability

Performs a cURL request to check if your website is online.

The PHP code snippets uses PHP cURL (Client URL Library). This function takes a domain name as input parameter and outputs TRUE or FALSE (for available or unavailable), depending on the returned HTTP status code.

Protip

Speaking about cURL, learn why not to turn off CURLOPT_SSL_VERIFYPEER in your PHP configuration.

As you know, the HTTP status code 200 means OK, and 304 is Not Modified. Those are the status codes we are looking for as anything else may indicate a problem with your website or server.


<?php
/**
 * PHP/cURL function to check a web site status. If HTTP status is not 200 or 302, or
 * the requests takes longer than 10 seconds, the website is unreachable.
 * 
 * Follow me on Twitter: @HertogJanR
 * Send your donation through https://www.paypal.me/jreilink. Thanks!
 *
 * @param string $url URL that must be checked
 */
function url_test( $url ) {
  $timeout = 10;
  $ch = curl_init();
  curl_setopt ( $ch, CURLOPT_URL, $url );
  curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
  curl_setopt ( $ch, CURLOPT_TIMEOUT, $timeout );
  $http_respond = curl_exec($ch);
  $http_respond = trim( strip_tags( $http_respond ) );
  $http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
  if ( ( $http_code == "200" ) || ( $http_code == "302" ) ) {
    return true;
  } else {
    // return $http_code;, possible too
    return false;
  }
  curl_close( $ch );
}
 
$website = "www.example.com";
if( !url_test( $website ) ) {
  echo $website ." is down!";
}
else { echo $website ." functions correctly."; }
?>

Change www.example.com with your domain name. This can easily be scheduled in a Linux/Unix cron or Windows scheduled task, if you have PHP installed locally.

This may interest you:   Custom PHP version on IIS Express and WebMatrix 3

Send an email when your website is down

And what if your website is down? Send an email! :-) The above PHP function is very easy to extend with a PHP mail send function. That keeps you informed on any downtime.

All can be automated using a monitoring tool like Monit. Read more about Monit website and services monitoring, built on an Ubuntu 14.04 LTS VM on Windows Server 2012 R2 Hyper-V

If you enjoyed this article, then please consider making a donation.
Thank you for your support. ♥