You are here: Home / Topics / How to Check Website Availability with PHP

How to Check Website Availability with PHP

Filed under: PHP on 2022-10-30 21:30:30

Hello programmers, 

In this post, we are going to see how we can check website is active or not. We will make it possible with PHP and Curl. The following PHP script helps you to check if your website is online and available. If you want to check the status of your website, make a cURL request to check if the website is available or online.

Check any website's availability online here

PHP Program for checking website availability

<?php  
function checkWebSite($url){   
// Check if the URL provided is valid    
if(!filter_var($url, FILTER_VALIDATE_URL)){      
return false;   
 }    
// Initialize cURL    
$ch = curl_init($url);        // Set options    
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10);    
curl_setopt($ch,CURLOPT_HEADER,true);    
curl_setopt($ch,CURLOPT_NOBODY,true);    
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);    // Get the answer    
$response = curl_exec($ch);        // Close the cURL session    
curl_close($ch);    
return $response ? true : false;  
}  
$url = 'https://google.com';  
if(checkWebSite($url)){   
 echo 'The web site is available.';        
}
else{    
 echo 'The web site is not available';  
 }
?>

 

Output of this program will be: 

The web site is available.

About Author:
T
Team MCQ Buddy     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.