blue elephant plush toy on black laptop computer

Check website availability with PHP and cURL

A useful PHP script to check if your website is up and online available. It uses PHP and cURL. This script comes in handy because website uptime and availability is important, and you want your website to be always online available.

Home » Check website availability with PHP and cURL

Here you’ll find a PHP script to check if your website is up and online available. It uses PHP and cURL. This script comes in handy because website uptime and availability is important, and you want your website to be always online available. If your website is down, it’ll send you an email to notify you about downtime.

The PHP code to check if your website is up 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.

You can use this code to check if your website is online available. Schedule in a cron for automated checks.

Also read:

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: @Jan_Reilink
 * 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 {
		// you can return $http_code here if necessary or wanted
		return false;
	}
	curl_close( $ch );
}

// simple usage:
$website = "www.example.com";
if( !url_test( $website ) ) {
	echo $website ." is down!";
} else {
	echo $website ." functions correctly.";
}
?>

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

See also  8 Tips to improve Joomla performance

Send an email if 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 sending function. This’ll keep 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. You can also set up and configure Monit in Windows WSL to monitor your websites easily.

One-time donation

Please take a second to support Sysadmins of the North and donate, your generosity helps!

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments