Website online-offline checker using PHP and cURL (Client URL Library)

Website online-offline checker using PHP

In this tutorial you will learn how to create a website online-offline checker using PHP and cURL.

This can be easily achieved with just few lines of code. What we need to do is to make a request to the website in question and see, if it’s server is responding. If it is – it’s most likely online.

As I mentioned above, it is easy as 25 lines of code. You can use this method with AJAX call as well.

* Disclaimer: The below check can be achieved with a lot of different methods. For this tutorial I choose cURL, as it’s more interesting

Let’s get started and create our website online-offline checker using PHP (ofcourse). The main purpose is to check, if the web server is responding to our request.

[code language=”php”]

// Our main function
function checkIfOnline($url) {
// Setting the url
    $curlInit = curl_init($url);
// Set the timeout
    curl_setopt($curlInit, CURLOPT_CONNECTTIMEOUT, 10);
// passing the headers to the data stream 
    curl_setopt($curlInit, CURLOPT_HEADER, true);
// Doing a request without getting the body
    curl_setopt($curlInit, CURLOPT_NOBODY, true);
// TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly
    curl_setopt($curlInit, CURLOPT_RETURNTRANSFER, true);

    // Get response
    $status= curl_exec($curlInit);

    if ($status) {
        return true;
    } else {
        return false;

// Call the function and get the result – you can pass the URL as a parameter in a GET request
if (checkIfOnline(‘’)) {
    echo "Up";
} else {
    echo "Down"; // Or return an error code

If you follow this tutorial step by step, you have your own working website online-offline checker using PHP and cURL.

If you have any questions and suggestions, feel free to leave a comment below.

Leave a Reply