If you have ever administered a website, you know that downtime is unavoidable. Creating a system to automatically check if your website is up can help deal with downtime and provide a better user experience. Doing so is quite simple if you have the following tools available:

  • A web server other that that on which the website is hosted
  • Cron (if you do not have this, you can use an online service)
  • PHP with libcurl support
  • PHP with mail support (optional)

Setting up PHP and libcurl

In case you don’t know, libcurl (and its cousin cURL) are tools for working with URLs and web pages. Provided that you have libcurl support and PHP, you can check if a site is active by using the following function:

function checkURL($url) {
	$return = array();
	$curl = curl_init(); // Initialize libcurl
	// set options:
	curl_setopt ($curl, CURLOPT_URL, $url ); // URL to visit
	curl_setopt ($curl, CURLOPT_RETURNTRANSFER, TRUE); // returns a string instead of echoing to screen
	curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); // follows redirects (recursive)
	curl_setopt($curl, CURLOPT_NOBODY, TRUE); // Only get headers, not content (saves on time)
	$result = curl_exec($curl);
	$errno = curl_errno($curl);
	if ( $errno != 0 ) { // curl_errno returns 0 if no error, otherwise returns the error code
		$return['message'] = "An error occurred while trying to $url! ".curl_err($curl); // If there was an error, return the error message
		$return['success'] = false;
	} else {
		$http = curl_getinfo($curl, CURLINFO_HTTP_CODE); // Get the HTTP return code
		$return['code'] = $http;
		if ( $http >= 200 && $http < 300 ) { // An HTTP code greater than 200 and less than 300 means a successful load
			$return['message'] = "$url is up! ($http)";
			$return['success'] = true;
		} else {
			$return['message'] = "$url is down! ($http)";
			$return['success'] = false;
        return $return;

This function returns an array containing a success boolean (true, your site is up or false, your site is down) and a corresponding message string. Now that our checking works as specified, let’s move on to the notifications.


In this section, we will present two ways to receive notifications.

Notification by E-mail/SMS

After checking the URL, you can use the PHP mail function to send the status to your e-mail account. On the other hand, you can find out the address for your SMS by sending a text to your own e-mail. For example, Verizon phones have the e-mail address <phone number>@vtext.com

The code:

$result = checkURL($your_url);
if ( $result['success'] == true; )
	$subject = "Your site is still up!";
	$subject = "Oh no! Your site is down."
mail($your_email, $subject, $result['message']);

As you can see, this is a fairly easy method with minimal code.

Push notifications for your iDevice

If you have an iDevice, such as an iPhone or iPod Touch, you can use push notifications as an alternative to e-mail or SMS.

First, download the app pushme.to and make and account. Take note of your username. This app will allow you to send pushed messages to your iDevice.

Then, you will need the following function:

function pushmeto($username, $message) {
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_URL, "http://pushme.to/$username/");
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
	curl_setopt($curl, CURLOPT_POST, TRUE);
	curl_setopt($curl, CURLOPT_POSTFIELDS, "message=".urlencode($message);

Usage is simple:

$result = checkURL($your_url);
pushmeto($your_username, $result['message']);


Cron can be used to automate the checking uptime process. If you don’t have cron on your server (which is rare), you can use an online service such as Online Cron Jobs.

Cron jobs are set using the following format:

[min] [hour] [date] [month] [day_of_week] [command]

A cron job set to run at 7am every Tuesday would look like this:

0 7 * * 2 [command]

For more information on how to use cron, see this quick reference.

As for the command, you can use the following (which assumes you saved the uptime verification code to a file called uptime.php):

php /path/to/uptime.php > /dev/null 2>&1

Remember, of course, to change the path.

Once activated by the automated command, the code in the PHP file will check if your site is up and notify you. You can set this to occur as often as you want.


Do you have any other ways to monitor uptime? If so, please join the discussion below!

Leave a Reply

Automated Uptime Verification