Check a url is valid or invalid with PHP | Web Programming Tutorial

Check a url is valid or invalid with PHP

Published Date: May 8, 2015 |
Published Time: 9:59 pm |

Hello Everyone. After a long day with a very simple topics but sometimes very usable. Today i will give a explanation how you can check a url or website is exists or not with PHP code. You can check with browser or through ping from command promt. But we may also check it with php programing.

Here i use PHP cURL library. Some one may ask what it is. I will discuss about it in another tutorial. Just one thing this library is so much powerful and useful in webworld.

This function return 1 or 0. If return 1 that means the url which you tested is valid or existed. Otherwise not.

What actually does this script ? Ok let me explain in brief.

First the script go to the url . Some parameter is given here. Everything has a meaning. But just care about the

CURLOPT_CONNECTTIMEOUT – timeout for the connect phase, That’s mean how many seconds the script try to connect with the url. After that time if no connection found, script will stop to try to connect with the url.

And after that   $ch = curl_init( $url )  line , means you have executed the curl command.

$get_info = curl_getinfo($ch); This line gives some information as response as array. One of them is http_code. If the code id 200, then the url is existed.  You can check about the meaning of http_code from here  http://en.wikipedia.org/wiki/List_of_HTTP_status_codes.

 

Let’s make a try.Click here to view demo Demo

Leave a Reply

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