我正在处理我的 404 错误文档,我在考虑的不仅仅是提供站点地图,还可以根据服务器上实际存在的内容向用户建议他可能正在寻找的网站。
示例:如果用户输入“www.example.com/foldr/site.html”,404页面可能会输出:
您的意思是“www.example.com/folder/site.html”吗?
为此,我编写了以下代码,非常适合我。我现在的问题是:使用它是否“安全”?因为基本上有人可以通过尝试各种组合来检测服务器上的所有文件。或者,黑客甚至可以使用循环遍历并列出所有类型的有效 URL 的脚本。
我应该限制这个脚本可以检测和建议的目录吗?使用一组“OK”位置,还是按文件类型?
有没有其他人已经有了这样的想法?
PHP:
// get incorrect URL that was entered
$script = explode("/",$_SERVER['SCRIPT_NAME']);
$query = $_SERVER['QUERY_STRING'];
// create vars
$match = array();
$matched = "../";
// loop through the given URL folder by folder to find the suggested location
foreach ($script as $dir) {
if (!$dir) {
continue;
}
if ($handle = opendir($matched)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
similar_text($dir, $entry, $perc);
if ($perc > 80) {
$match[$entry] = $perc;
}
}
}
closedir($handle);
if ($match) {
arsort($match);
reset($match);
$matched .= key($match)."/";
} else {
$matched = false;
break;
}
$match = array();
}
}
// trim and echo the result that had the highest match
$matched = trim(ltrim(rtrim($matched,"/"),"."));
echo "proposed URL: ".$_SERVER["SERVER_NAME"].$matched;