Search code examples
phpvalidationurlfilter-var

Why does FILTER_VALIDATE_URL consider this to be a valid URL?


For some reason that I don't understand, FILTER_VALIDATE_URL says the following URL is valid:

http://ghjfgh

Don't all valid URLs contain at least one period? I've never seen a TDL that didn't have one by definition. So why does PHP say it's valid?

Here's the code. You can quickly run it on phpfiddle.org for yourself:

<?php
$URL = "http://ghjfgh";

    if($URL != "" && !filter_var($URL, FILTER_VALIDATE_URL)) {
        $error = "Please enter a valid URL";
    } else {
        $error = "All good";
    }

echo $error;
?>

Solution

  • It filters according to RFC 2396, and http://ghjfgh is valid according to that spec. An easy example would be http://localhost which is obviously valid (as @johnconde pointed out in the comments)