The purpose of this post is to show you how to determine the status (primarily
offline) of a remote webpage using cURL and other methods. This post might be best read in company with another article "Resolve Short URLs To Their Destination URL with PHP". For the sake of it, a small WordPress shortcode function will follow at the end of the post that will cache the status (online or offline) of a remote website for a period of 10 minutes.
Online or Offline
Let's assume we wanted to check on the status of this website by reading the returned HTTP header. This function uses
curl_getinfo to retrieve the header response code that it uses to determine the status of the remote website.
if($httpcode >= 200 && $httpcode < 300) - and, in particular, the
300 code - can be modified to return the status of the remote website based on your own requirements (maybe just
The status range if fairly generic in nature and doesn't give us a complete picture of the remote site. For that reason, I return the remote HTTP status code and return a more applicable message (see the function below).
Remote Status Code
The following code will return only the remote header status code. It's essentially identical to the function above with the exception that we're returning a code rather than testing a range.
1xx header messages are informational.
2xx header messages indicate a success.
3xx header messages indicate a redirect.
4xx header messages indicate a client error.
5xx header messages indicate a server error.
Array of Header Data
To return an array of remote header data, you could use something like this:
In the above example, I've also directly printed the Server Response from the array (
Other Methods To Check Remote Website
There are countless means of checking the status of a remote website or retrieving remote header data. Here's just a few different ways of accomplishing the same thing we've done up the top of this page with cURL.
PHP's get_headers() Function
The following will use PHP's get-headers() function and return
Offline (with online determined by a 200, 301 or 302 header status code).
Using the same function you can just as easily return an array of the remote header.
 => HTTP/1.0 200 OK
 => Date: Sun, 26 Aug 2012 05:49:33 GMT
 => Server: Apache
 => X-Pingback: http://www.url.com/xmlrpc.php
 => Set-Cookie: wpfilebase=1
 => Vary: Accept-Encoding
 => Connection: close
 => Content-Type: text/html; charset=UTF-8
PHP's $http_response_header & stream_get_meta_data() Function
When using a HTTP wrapper ,
$http_response_header will be populated with the HTTP response headers (similar to
Both are less efficient than PHP's
get_headers() function. You could also consider using PHP's getallheaders() , headers_list() or apache_request_headers() function. There are other PHP Network Functions that may also serve your particular purpose.
PHP's fsockopen() Function
By opening a socket with fsockopen() and checking for a connection we can obtain limited data. It's handy if your server doesn't support cURL.
Should you choose to reference the status of a remote website from within a WordPress post or page, you can use the following shortcode.
Copy and paste the WordPress function into your theme's
functions.php file or, if you sensibly have one installed, your custom functions plugin. You may optionally download and install our plugin from the bottom of of the page.
If you've define a default URL in your shortcode attributes, you can simply use
[httpstatus]. The shortcode will return text as defined in your attributes (Online or Offline). Should you choose to reference another URL, you would simply do so as follows:
Available shortcode attributes are
$online message, and
PHP Function (Simple Cache)
A working PHP function to be used outside of WordPress is as follows. Usage of Simple Cache is required.
- Consider replacing online/offline text with images.
- You should consider caching the responses for at least a few minutes (as we've done with our WP and final PHP function) to avoid making repeated requests to the same site.
- Randomize user agents with this function.