php

How to Check Website Availability with PHP

In this tutorial, we are going to see how to check website availability with PHP. The following PHP script helps you to check if your website is online and available. It uses PHP and cURL. cURL is the easiest option to check the availability of a website. If you want to check the status of your website, make a cURL request to check if the website is available or online.
 

How to Check Website Availability with PHP
<?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://stackhowto.com';

	if(checkWebSite($url)){
		echo 'The web site is available.';      
	}else{
	   echo 'The web site is not available'; 
	}
?>

Output:

The web site is available.
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *