使用filter_var验证具有或不具有协议的 URL

2022-08-30 23:45:59

我正在尝试使用PHP的扩展进行验证。每 http://php.net/manual/en/filter.filters.validate.phpfilter_var()

验证值作为 URL(根据 » http://www.faqs.org/rfcs/rfc2396),可选择使用必需的组件。请注意,有效的URL可能没有指定HTTP协议 http:// 因此可能需要进一步的验证来确定URL使用预期的协议,例如 ssh:// 或mailto:。请注意,该函数只会找到有效的ASCII URL;国际化域名(包含非 ASCII 字符)将失败。

关于,请注意有效的URL可能没有指定HTTP协议,我下面的测试表明HTTP协议是必需的()。我如何误解文档?URL 'stackoverflow.com/' is NOT considered valid.

此外,如何阻止诸如 https://https://stackoverflow.com/ 之类的 URL 验证 true?

PS.任何关于我对协议进行消毒的方法的评论将不胜感激。

<?php
function filterURL($url) {
    echo("URL '{$url}' is ".(filter_var($url, FILTER_VALIDATE_URL)?'':' NOT ').'considered valid.<br>');
}
function sanitizeURL($url) {
    return (strtolower(substr($url,0,7))=='http://' || strtolower(substr($url,0,8))=='https://')?$url:'http://'.$url;
}

filterURL('http://stackoverflow.com/');
filterURL('https://stackoverflow.com/');
filterURL('//stackoverflow.com/');
filterURL('stackoverflow.com/');
filterURL(sanitizeURL('http://stackoverflow.com/'));
filterURL(sanitizeURL('https://stackoverflow.com/'));
filterURL(sanitizeURL('stackoverflow.com/'));

filterURL('https://https://stackoverflow.com/');
?>

输出:

URL 'http://stackoverflow.com/' is considered valid.
URL 'https://stackoverflow.com/' is considered valid.
URL '//stackoverflow.com/' is NOT considered valid.
URL 'stackoverflow.com/' is NOT considered valid.
URL 'http://stackoverflow.com/' is considered valid.
URL 'https://stackoverflow.com/' is considered valid.
URL 'http://stackoverflow.com/' is considered valid.
URL 'https://https://stackoverflow.com/' is considered valid.

答案 1

FILTER_VALIDATE_URL使用 ,它不幸地解析为有效的URL(因为考虑到URI RFC,它确实是一个有效的URL):parse_url()'https://https://'

var_dump(parse_url('https://https://stackoverflow.com/'));

array(3) { 
  ["scheme"]=> string(5) "https" 
  ["host"]=> string(5) "https"
  ["path"]=> string(20) "//stackoverflow.com/" 
}

您可以将函数更改为:sanitazeURL

function sanitizeURL($url) {
  return (parse_url($url, PHP_URL_SCHEME)) ? $url : 'http://' . $url;
}

但是您仍然必须检查主机名是否不是:httphttps

function filterURL($url) {
  echo("URL '{$url}' is ".((filter_var($url, FILTER_VALIDATE_URL) !== false && (parse_url($url, PHP_URL_HOST) !== 'http' && parse_url($url, PHP_URL_HOST) !== 'https'))?'':' NOT ').'considered valid.<br>');
}

答案 2

您可以删除 http 或通过验证它是否存在来添加它。

<?php
$url = "http://www.nigeriatest.com";

// Remove all illegal characters from a url
$url = filter_var($url, FILTER_SANITIZE_URL);

// Validate url
if (!filter_var($url, FILTER_VALIDATE_URL) === false) {
    echo("$url is a valid URL");
} else {
    echo("$url is not a valid URL");
}
?>

推荐