Why FILTER_VALIDATE_URL return FALSE for only this url?

末鹿安然 提交于 2020-01-24 13:54:16

问题


i have the following code:

$url = "http://icons3.iconfinder.netdna-cdn.com/data/icons/pool/poolbird.png";

if (filter_var ($url, FILTER_VALIDATE_URL) === FALSE) {

    echo "Invalid Url";
    exit;

} else {

    echo "Works!";
}

This displays:

invalid url (FALSE)

for the above url, but not for other simpler urls. Is this a bug? you can even access the image.

And the most important is what's the solution for this?

Thanks


回答1:


PHP < 5.2.13 contains a bug in FILTER_VALIDATE_URL that considers urls containing the '-' (http://bugs.php.net/51192). You either need to upgrade your copy of php or use a different filtering mechanism.




回答2:


That code prints "Works!" for me. What version of PHP are you using? Can you post a link to a PHP page containing:

<?php phpinfo(); ?>

Also, see this question.




回答3:


Running PHP 5.2.13, I get this response when trying to filter a valid URL:

php > var_dump(filter_var("http://www.asdf-asdf.com", FILTER_VALIDATE_URL));
bool(false)

This is clearly not what I woukd expect it to return. The domain is valid.

This has evidently been fixed now, and will be included in PHP 5.3.3 and 5.2.14, more info in this bugreport: http://bugs.php.net/bug.php?id=51192




回答4:


The source archive from http://www.php.net/get/php-5.3.2.tar.bz2/from/a/mirror contains a strange version of ext/filter/logical_filter.c, a version I can't find in the cvs. This version performs an "extra" test on the host part of the url

e = url->host + strlen(url->host);
s = url->host;

while (s < e) {
  if (!isalnum((int)*(unsigned char *)s) && *s != '_' && *s != '.') {
    goto bad_url;
  }
  s++;
}

which doesn't allow a hyphen in the host name like in icons3.iconfinder.netdna-cdn.com
Very strange and very wrong.



来源:https://stackoverflow.com/questions/2596517/why-filter-validate-url-return-false-for-only-this-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!