Link Verifier - Check If a URL is Valid or Not
33,464 ViewsPHP Tutorials March 9th, 2007
This is actually quite a neat little bit of code that can come in handy in many, many different scenario's. Learn how to check if an URL is valid or not (404). If it is valid, a success message can be output, and if it is not valid (404) then an error message is put out.
This can be very useful when writing tutorial indexing scripts, the user can make sure there URL works before they submit there script, or in many many other situations!
Alright, for such a versatile thing like a link verifier I am going to pretty much give you the code, explain how it works and then at the end of all this I give you and explain to you a little script I threw together that uses a form to check URL's.
-
$url = 'http://www.UrlHere.com';
That is pretty much the basis of your code, and what you will be using. The way you use it though, is how you can be creative. First I will explain that little bit of code though.
First we start off by setting our $url varialbe, this is what we use inside of our fsockopen() function. We could set the URL straight inside the function but it is better coding practice to assign it to a variable. You can get your variable in many different ways, we will look into that later.
Next we assign our function, to a variable so that later on we can check if our function succeeded or failed. Now, this is where the heart of it comes in, our function that checks the URL. As you can see, we put a @ sign in front of the function, this stops it from printing out ugly errors to the user if it doesn't work.
If we didn't put that @ sign, and the link was not valid, it would print out some really long ugly PHP error, good if you are developing maybe but not when you are using a live site. We are going to create our own, pretty errors!
The only thing you will need to change in that functions parameters is the $url part, if you change your URL's variable to something different, you will need to change that too.
So let's say you just wanted to create a simple link checker script, where the user inputs a URL into an input box, clicks submit and a success or error message is outputted depending on the outcome. This can be very easily done.
-
<?php
-
-
$url = $_POST['url'];
-
-
$page = $_SERVER['PHP_SELF'];
-
-
if (!$valid) {
-
-
// Output Error Message
-
<p><span style="color:#EE0000">Sorry, but that link is <b>not</b> valid.</span></p>
-
<p>Would you like to <a href="'.$page.'">check another link?</a>';
-
-
} else {
-
-
// Output Success Message!
-
<p><span style="color:#458B00">The link you have entered, is valid!</span></p>
-
<p>Would you like to <a href="'.$page.'">check another link?</a>';
-
-
}
-
} else {
-
?>
-
<h3>Link Verifier</h3>
-
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
-
<p><b>URL :</b> <input type="text" name="url" size="30" value="Input Link" /></p>
-
<div align="center"><input type="submit" name="submit" value="Check" /></div>
-
<input type="hidden" name="submitted" value="TRUE" />
-
</form>
-
<?php
-
}
-
?>
As you can see, I wrote a small link checker script, user puts in a URL and a message is output, nothing fancy but it works! You can download the source for this entire script at the bottom of this page.
This is the starting of our script, we use an if statement to check if the form was submitted, if it was submitted then we continue on. We grab our URL and put it into a variable. The $_POST['url'] is the name of the input we use in our form at the bottom of the script.
Then we check if the link is or isn't valid, we set this to a variable for ease of use after this, and then we get the current file name of the page we are on, just so we can create links (for fun) after
-
if (!$valid) {
-
-
// Output Error Message
-
<p><span style="color:#EE0000">Sorry, but that link is <b>not</b> valid.</span></p>
-
<p>Would you like to <a href="'.$page.'">check another link?</a>';
This is where we output are error message, first we show off the URL to the user, in case they forgot, (which I hope they didn't forget too easy). Then, since we used !$valid, in our if statement that means if it is NOT valid (The variable $valid = FALSE in other words) then we put out a red error message, saying it doesn't work. This is where we use the $page variable and make a link so the user can easily go back to the form, and try it again.
-
} else {
-
-
// Output Success Message!
-
<p><span style="color:#458B00">The link you have entered, is valid!</span></p>
-
<p>Would you like to <a href="'.$page.'">check another link?</a>';
-
-
}
-
} else {
-
?>
Now we do the else part of our if statement, this is the exact same as the error message, except it's a success message! Then we also do the else part of our main if statement (the if statement for whether the form was submitted or not) and end our PHP document.
-
<h3>Link Verifier</h3>
-
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
-
<p><b>URL :</b> <input type="text" name="url" size="30" value="Input Link" /></p>
-
<div align="center"><input type="submit" name="submit" value="Check" /></div>
-
<input type="hidden" name="submitted" value="TRUE" />
-
</form>
-
<?php
-
}
-
?>
Now we just make our form, make sure to label the URL input as url, or else it won't work.
That's about all!
Feel free to leave a comment with any questions or anything, also if you want some help implementing this into your site don't be shy to ask!
Thanks,
Sean

(10 votes, average: 4.9 out of 5)










March 14th, 2007 at 10:59 pm
awesome, once again
March 20th, 2007 at 7:20 pm
When I made a script similar to this, if the users posts a link with http:// in there, the link isn't valid, although a correct link is http://www.site.com.
I used the str_replace function, so if a URL with http:// is posted it still works.
Hope this helps.
Other than that, nice script
Lee
April 1st, 2007 at 12:19 am
Can you make this into a Wordpress plugin which will validate the links in the link manager?
April 1st, 2007 at 6:26 pm
Haha, if I knew how to code wordpress plugins, then I more certainly would!
July 23rd, 2007 at 3:12 am
anyone here know how to detect a 404 error code?
September 8th, 2007 at 2:28 pm
cheers man, just what i've been wondering how to do! i'm gonna do a little AJAX type link checker so people using my form can check if their link will work before they submit it. Cheers once again!
October 16th, 2007 at 12:05 pm
thanks ! i was looking for such code..it's very simple to understand
April 28th, 2008 at 4:03 pm
There's a bug.
If you request a url that apache handles and redirects, it returns false, although for checking if a URL is valid, it should return true.
May 10th, 2008 at 5:24 am
If destination site use Apache 'mod_rewrite' the scripts will output - ... not valid!
August 27th, 2008 at 12:26 pm
It does not work (PHP4 only?)
Try http://www.roscripts.com/snippets/show/156
August 31st, 2008 at 8:45 pm
Hello!,
January 5th, 2009 at 9:59 am
Thanks for information
May 9th, 2009 at 3:48 am
thats great! i searched it for hours. thanks
November 30th, 2009 at 5:27 am
Good Script, but can we check type of link, like zip, exe or html
January 4th, 2010 at 10:29 pm
Thanks for the help in this question. All ingenious is simple.
January 8th, 2010 at 10:31 am
Sch
January 25th, 2010 at 1:04 am
I just stopped by your blog and thought I would say hello. I like your site design. Looking forward to reading more down the road.
January 25th, 2010 at 1:21 am
Hey, thanks for this!
February 16th, 2010 at 8:16 am
Is there a way to check from a list of links, whether they are valid or not. Automating it!!!
Or is there something which can show you all the links available in a subdirectory of website.
February 20th, 2010 at 8:26 am
got this very nice, but what if i have to take input from address bar to check that url for my website, can you guide me in that case.
thank you in advance.
March 5th, 2010 at 7:12 pm
Adding a bit more details on the topic would attract more readers. I think you are on a good start