PHP: URL validation

This entry is part 53 of 54 in the series PHP Tutorial

If we are to validate a web address or URL entered using PHP, we will have to ensure that users will also type in http:// in front of the web address.

<html>
<body>
<form action="" method="post">
    URL (Pls  include http://): <input type="text" name="url"><br><br>
    <input type="submit" name="submit">
</form>
</body>
</html>


<html>
<body>

<?php
$url = @$_POST["url"];

if (isset($_POST['submit'])) {
    if (filter_var($url, FILTER_VALIDATE_URL)) {
        echo "The valid URL is " . $url;
    } else {
        echo "This ($url) web address is considered invalid.";
    }
}
?>

</body>
</html>

You can try entering some URLs in the form here.

Series Navigation<< PHP: Form email validation
PHP: Passing variables from Javascript to PHP >>