Validate the URL Link using PHP
In this tutorial, we are going to learn
how to Validate the URL Link using PHP. Do you want to validate or verify the URL if it is valid or not? You are at the right place to check it. To verify the URL that you have kindly entered into the text box provided on the web page then the PHP source code or the validation tool will do the rest to check it.
To check the URL is very important if it is valid or not before the user taking any action on it. You can also check the live demo of this simple tutorial, so you can get an idea and you can try this out, let's start coding.
Creating Simple Markup
This HTML source code where the user can enter the URL to check or to validate if it is valid or not before they taking any action on it.
<p align="center" class="error_message">
<?php echo $error_message; ?>
<span class="text_style">Enter URL link
</span>
<input type="text" class="text_box" name="url_link" placeholder="Type URL to validate . . . . ." autofocus="autofocus" required />
<input type="submit" class="btn_style" name="enter" value="Validate URL">
PHP Query
This simple query that we are going to use for the validation check of the URL enters by the user in the text box on the webpage.
<?php
if(isset($_POST['enter'])){
$url_link=$_POST['url_link'];
if (!filter_var($url_link, FILTER_VALIDATE_URL
) === false) {
$error_message = "<b style='color:blue;'>$url_link</b> is a valid URL";
} else {
$error_message = "<b style='color:blue;'>$url_link</b> is not a valid URL";
}
}
?>
Error Message
This simple markup will display to the web page as a message alert to the user if the URL is valid or not.
<p align="center" class="error_message">
<?php echo $error_message; ?>
Output
This is simple text box and button where the user can type the URL Link to validate.

If the URL is valid, this would be the output.

And, if the URL is invalid.
Learn by Examples
Examples are better than 500 words. Examples are often easier to understand than text explanations. Kindly click the
"Download Code" button below for the full source code.
If you are interested in programming, we have an example of programs that may help you even just in small ways.
Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at
[email protected]. Practice Coding. Thank you very much.