How to Check Website Availability with PHP

Uptime is very important for a website and the owner of the website wants to make their site always online. To make sure the website is available or down, availability check need to be performed. You can use PHP to make the website availability check process quick and easy. The website availability status can be checked from the PHP script and notify about the downtime if the website is down.

The cURL is the easiest option to check website availability with PHP. If you want to check the website’s server status, perform a cURL request to check if the website is available or online.

Check if Website is Available

The following code snippets are used to check the domain availability using PHP cURL and show the status of the website. For the better usability, all the code are grouped in a function called isSiteAvailible().

The isSiteAvailible() function performs a cURL request with PHP and checks if the domain is available and online. Return TRUE if the specified website is available, otherwise, return FALSE if the website is offline.

function isSiteAvailible($url){
    
// Check, if a valid url is provided
    
if(!filter_var($urlFILTER_VALIDATE_URL)){
        return 
false;
    }

    
// Initialize cURL
    
$curlInit curl_init($url);
    
    
// Set options
    
curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,10);
    
curl_setopt($curlInit,CURLOPT_HEADER,true);
    
curl_setopt($curlInit,CURLOPT_NOBODY,true);
    
curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true);

    
// Get response
    
$response curl_exec($curlInit);
    
    
// Close a cURL session
    
curl_close($curlInit);

    return 
$response?true:false;
}

Uses:
Call the isSiteAvailible() function and pass the website URL that you want to check. Based on the response, you can display the online or offline status of the website.

<?php

$URL 
'https://www.codexworld.com';

if(
isSiteAvailible($URL)){
    echo 
'The website is available.';      
}else{
   echo 
'Woops, the site is not found.'
}

?>

3 Comments

  1. That_BOI Said...
  2. Subham Kumar Said...
  3. Kalpana Said...

Leave a reply

keyboard_double_arrow_up