我希望每个人都可以输入数据库的 url 或域,但我想用真正无法破解自己的路径过滤该域或 url,所以我的代码正确吗?
<?php
$url = $_GET['url'];
if (!filter_var($url, FILTER_VALIDATE_URL)) {
echo '*error*';
exit;
}
?>
This is I want [Y]: http://google.com
This is I want [Y]: http://google.com/index.php
This is I want [Y]: https://google.com
This is I want [Y]: https://google.com/index.php
This is I don't want [N]: google.com
This is I don't want [N]: google.com/index.php
谢谢大家。
它会起作用,但这不是一个好主意:
http://www.d-mueller.de/blog/why-url-validation-with-filter_var-might-not-be-a-good-idea/
总结:此功能存在安全问题,例如XSS(跨站点脚本)攻击,可能会伤害访问您网站的人(包括您自己)。它接受像脚本警报(123)这样的网址;
这是来自网站的解决方法,不是perfekt,但比普通filter_var更好:
function validate_url($url)
{
$url = trim($url);
return ((strpos($url, "http://") === 0 || strpos($url, "https://") === 0) &&
filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED | FILTER_FLAG_HOST_REQUIRED) !== false);
}