Monday , 20 November 2017

How to Check If Website Is Up or Down Using PHP

Check Website Is Up or Down

This is a programming tutorial for PHP beginner developers who want to develop a simple web app which can check if website is up or down using PHP programming language if you are a PHP programmer then must read this tutorial and learn something new which is cURL yes I am going to use cURL to set connection between your server and requested website server to see if website is up or down by getting HTTP response code.

PHP Script: Check If Website Is Up or Down

I am going to paste the code below and I will try my best to describe every single line of code yes there are few lines of code with two parts in the first part I used PHP and in the second part I used HTML to show form with a box and a button so that users can type in domain in the box and click on button to check if website is up or down it’s just for the user friendly environment otherwise I could use that users have to type in the URL in the address bar of their browsers using $_GET['url'] or any other method but typing in the box and clicking the button is the most easiest way for newbies. I have talked too much so now let’s start the real tutorial the code is pasted below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
	if (isset($_POST['submit'])) {
		$url = $_POST['url'];
		$curl = curl_init($url);
		curl_setopt($curl, CURLOPT_NOBODY, true);
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
		curl_exec($curl);
		$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
		if ($code == 200) {
			echo "<h3>Up!</h3>";
		} else {
			echo "<h3>Down!</h3>";
		}
	}
?>
 
<form action="" method="post">
	Url: <input type="text" name="url" /><br />
	<input type="submit" name="submit" />
</form>
  • From line no. 17-20 I coded HTML to show a text box and a button so that users can type in the domain in the box and click the button to parse the data on the same page because action=”” it means after clicking the submit button the data will be sent on the same page.
  • I hope you are beginner in PHP and you know basics about PHP like how to make and call variables or using IF-ELSE statements etc so I am not going to talk about it.
  • On line number 4 I have started cURL means the cURL session is initialized with requested URL because I added $url as a parameter value in the function.
  • Line number 5 says that the cURL don’t have to extract body of the requested domain or URL.
  • Line no. 6 tells the cURL to follow all redirection like if website is going to be redirected from naked domain to www then it will follow it without any trouble it is important to add this line.
  • On line number cURL is executed.
  • One line no. 8 I used a cURL built-in function to get HTTP code of requested domain or URL it is required actually this is the main part which helps us to check if website is up or down because we know that 200 code means that website is up.
  • Then from line no. 9-13 I used IF-ELSE statement to echo Up! if HTTP code is 200 otherwise just echo Down! so it’s easy to understand the IF-ELSE statement.

How to Test It!

Simply just copy the code above and paste it in any code editor or text editor then save it with any name but just remember to make its extension .php then upload it to any hosting account you have which supports PHP language and test it yourself if you see any error then tell us about it in the comments.

Need Any Help?

If you need any help or have any question then you are totally free to ask anything in the comments below I will reply you as soon as I can. If you like this tutorial then please give feedback and share it on your social profiles for us!

Leave a Reply