我在我的網站上對目錄中的檔案進行了簡單的搜索。其中的所有檔案看起來像example.html.
我已經讓用戶無法搜索空輸入,我不想阻止用戶搜索$exclude字串中的所有內容。否則,如果用戶輸入示例點,它將顯示目錄中的每個檔案。
這是我的 php 代碼:
$dir = "data/pages/";
$exclude = array('.','..','.htaccess','index');
if(array_key_exists('submit', $_POST)) {
if (empty($_POST['field1'])) {
echo("<p><h3>Please fill all the fields</h3>");
}
else {
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
echo("<p><h3>↓Search Results↓</h3>");
while (($file = readdir($dh)) !== false) {
$filename = pathinfo($file, PATHINFO_FILENAME);
if(preg_match("/{$_POST['field1']}/i", $filename) &&!in_array($file,$exclude)) {
echo("<p>Found: <b><a href=\"https://mywebsite.wtf/data/pages/" . $file . "\">$filename</a>");
}
}
closedir($dh);
}
}
}
}
uj5u.com熱心網友回復:
根據匹配(完全匹配或包含),您可以這樣做:
如果輸入包含一些排除:
$isOk = true;
$exclude = ['.','..','.htaccess','index'];
foreach ($exclude as $exclude){
if(str_contains($_POST['field1'], $exclude)){
$isOk = false;
}
}
如果輸入包含一些精確的輸入
$isOk = true;
$exclude = ['.','..','.htaccess','index'];
if(in_array($_POST['field1'], $exclude, true)){
$isOk = false;
}
在您的代碼中,進行精確檢查:
$dir = "data/pages/";
$excludes = array('.','..','.htaccess','index');
$isOk = true;
if(in_array($_POST['field1'], $excludes, true)){
$isOk = false;
}
if(array_key_exists('submit', $_POST)) {
if (empty($_POST['field1'])) {
echo("<p><h3>Please fill all the fields</h3>");
}
if (!$isOk) {
echo("<p><h3>You cannot search that!</h3>");
}
else {
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
echo("<p><h3>↓Search Results↓</h3>");
while (($file = readdir($dh)) !== false) {
$filename = pathinfo($file, PATHINFO_FILENAME);
if(preg_match("/{$_POST['field1']}/i", $filename) &&!in_array($file,$exclude)) {
echo("<p>Found: <b><a href=\"https://mywebsite.wtf/data/pages/" . $file . "\">$filename</a>");
}
}
closedir($dh);
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/389879.html
