How to Validate the URL Link using PHP

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.
  1. <div class="container">
  2.         <form method="post">
  3.                 <p align="center" class="error_message">
  4.                         <?php echo $error_message; ?>
  5.                 </p>
  6.                 <p align="center">
  7.                         <span class="text_style">Enter URL link</span>
  8.                         <input type="text" class="text_box" name="url_link" placeholder="Type URL to validate . . . . ." autofocus="autofocus" required />
  9.                 </p>
  10.                 <p align="center">
  11.                         <input type="submit" class="btn_style" name="enter" value="Validate URL">
  12.                 </p>
  13.         </form>
  14. </div>

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.
  1. <?php
  2. if(isset($_POST['enter'])){
  3. $url_link=$_POST['url_link'];
  4.  
  5. if (!filter_var($url_link, FILTER_VALIDATE_URL) === false) {
  6.     $error_message = "<b style='color:blue;'>$url_link</b> is a valid URL";
  7. } else {
  8.     $error_message = "<b style='color:blue;'>$url_link</b> is not a valid URL";
  9. }
  10. }
  11. ?>

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.
  1. <p align="center" class="error_message">
  2.         <?php echo $error_message; ?>
  3. </p>

Output

This is simple text box and button where the user can type the URL Link to validate. Result If the URL is valid, this would be the output. Result And, if the URL is invalid. Result

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.

Comments

Improve your strength, speed, Surmountway&weightedvestscheapsssu_123and performance with our selection of weighted training vests.Weighted vests are covered in pockets on the outside and come with a bunch of small, brick-shaped weights that fit in them. Training with the best weighted vest can greatly improve your overall fitness by challenging workouts that are both strengths and opportunities. Weighted vests are vests made of leather or vinyl, which have pockets in which you place the weights that you would like. [url=https://www.amazon.com/SurmountWay-Weighted-Adjustable-trength-Training/dp/B08R563JR4/ref=sr_1_78?dchild=1&keywords=weight+vest+for+men&qid=1631584366&sr=8-78][/url]

Add new comment