filter_var对于URL不正确

i'm trying to use filter_var to validate URLs that have been entered by a user, but it seems not to work in all cases. I've requested a fix for this some time ago on PHP.net, but to no avail.

Besides regexes (yes, a few have been posted on stackoverflow), is there a PHP version that does work or any other method?

The problem I'm facing is that the following string get's validated, which should not:

http://http://www.google.com

Test script:

$url = $_REQUEST['url'];

if (filter_var("http://" . $url, FILTER_VALIDATE_URL)) 

   $filter="true"; 

else 

   $filter="false";

echo "Filter var result of concat with http is:" . $filter . "<br>";

Expected result:

I expect it to return false when i run the script with http://www.google.com and true when i run it with www.google.com

Actual result:

It returns true in both cases

If the bug you reported is anything to go by, filter_var() validates a url by parsing it parse_url() and checking whether the resulting array is sane.

Therefor, glue it back together and validate that they're equal if you want something more strict.

Here's a reasonable glue_url function:

http://doc.habariproject.org/inputfilter_8php_source.html#l00324

Then:

$valid_url = ($url == glue_url(parse_url($url)));

Optionally, also check for "http://$url" if no protocol is supplied.

I have a small class I've used for stricter URL validation. Might as well put it into the public domain:

https://github.com/franksrevenge/StrictUrlValidator

It relies on a few logic tests and then gets increasingly intrusive by testing whether the given hostname exists and even by querying the server for status code for the URL. The behavior for remote queries is configurable, of course.