Next Steps to Validate URL

Today I will show how to check url is working or not. Normally we are doing URL validation based on format of URL.Now days every everything should be perfect means if url is well formatted then our next step should be check it is working or not. If it’s working then it’s useful otherwise its garbage.
When you are working on image or video upload functionality before uploading we must check url is openable or not.
In this post I will create a function which will take url as parameter and check its working or not, based on result its return true or false.


We used @fopen($url,”r”) function of PHP to verify URL is live or not. if its return true that’s mean it’s working or valid otherwise its invalid.

Below is simple function to verify url is working or not:

1
2
3
4
5
6
7
8
9
10
function verify_url($url)  
{  
$check = @fopen($url,"r"); 
if($check)  
 $status = true;  
else  
 $status = false;  
   
return $status;  
}

How to use:

1
2
3
4
5
6
7
8
9
10
11
<?php  
$url = "http://www.google.com";  
if(verify_url($url))  
{  
 echo "<div>Congratulation ! Your URL <a href=$url>$url</a>  : is <b>valid </b></div>";  
}  
else  
{  
 echo "<div>Error ! Your URL : <a href=$url>$url</a> is <b>invalid </b></div>";  
}  
?>

Output: After call the function it will return
so after execution it will show the message as

Congratulation ! Your URL http://www.google.com is valid.

The following two tabs change content below.
Hi, This is Parvez Alam from India. I am software developer with 4 years’ experience in web development. I have submitted articles on PHP, Mysql, Magento,CSS, HTML, jQuery, web designing and social API. You can subscribe to my blog via RSS/Twitter/Google plus and Facebook. parvez1487(at)gmail(dot)com

Latest posts by Parvez (see all)

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>