我如何检查我的 post 字符串是否包含符号并阻止继续
How do i can check if my post string contains a symbol and prevent from proceeding
我正在我的网站上对目录中的文件进行简单搜索。
其中的所有文件看起来都像 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);
}
}
}
}
根据匹配项(完全匹配或包含),您可以这样做:
如果输入包含一些排除:
$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);
}
}
}
}
我正在我的网站上对目录中的文件进行简单搜索。
其中的所有文件看起来都像 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);
}
}
}
}
根据匹配项(完全匹配或包含),您可以这样做:
如果输入包含一些排除:
$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);
}
}
}
}