advertisement

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';
$valid = @fsockopen("$url", 80, $errno, $errstr, 30);

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
if (isset($_POST['submitted'])) {

	$url = $_POST['url'];
	$valid = @fsockopen("$url", 80, $errno, $errstr, 30);
	
	$page = $_SERVER['PHP_SELF'];

	if (!$valid) {
		
		// Output Error Message
		echo '<h3>'.$url.'</h3>
		<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!
		echo '<h3>'.$url.'</h3>
		<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.

<?php
if (isset($_POST['submitted'])) {

	$url = $_POST['url'];
	$valid = @fsockopen("$url", 80, $errno, $errstr, 30);

	$page = $_SERVER['PHP_SELF'];

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
		echo '<h3>'.$url.'</h3>
		<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!
		echo '<h3>'.$url.'</h3>
		<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

Link Verifier – Complete Source Code

advertisement