使用PHP清理获取输入。是不是太晚了,我还需要这么做?
我试图通过数组清理搜索中的用户输入。我想知道我是否以正确的方式对输入进行了消毒,我是否需要在这样的函数中使用它。你会得到它: 主要问题#1:我是在正确的地方以正确的方式对输入进行“消毒”,还是为时已晚 HTML->使用PHP清理获取输入。是不是太晚了,我还需要这么做?,php,get,httprequest,Php,Get,Httprequest,我试图通过数组清理搜索中的用户输入。我想知道我是否以正确的方式对输入进行了消毒,我是否需要在这样的函数中使用它。你会得到它: 主要问题#1:我是在正确的地方以正确的方式对输入进行“消毒”,还是为时已晚 HTML-> <div > <form class="theform"> Jeg vil gerne have: <input class="theinputbox" type="text" onkeyup="showHint(this.value)
<div >
<form class="theform">
Jeg vil gerne have: <input class="theinputbox" type="text" onkeyup="showHint(this.value)">
</form>
</div>
<div id="theHint"><span id="txtHint"></span></div>
包含数组和结果的PHP->
<?php
$a[] = "Anna";
$a[] = "Brittany";
$a[] = "Cinderella";
$q = test_input($_REQUEST["q"]);
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$hint = "";
// lookup all hints from array if $q is different from ""
if ($q !== "") {
$q = strtolower($q);
$len=strlen($q);
foreach($a as $name) {
if (stristr($q, substr($name, 0, $len))) {
if ($hint === "") {
$hint = '<p class="txtHint">' . $name . '</p>';
} else {
$hint .= '<p class="txtHint">' . $name . '</p>';
}
}
}
}
// Output "no suggestion" if no hint was found or output correct values
echo $hint === "" ? "no suggestion" : $hint;
?>
在您的特定情况下,您不需要消毒。您从不输出输入,因此不可能使用XSS;您从不在SQL语句中使用输入,因此不可能使用SQL注入
如果您想要清理,对于这种特定情况,您应该将您希望允许的字符列为白名单。因为您正在匹配名称,所以实际上只需要允许大写和小写字母。您可以通过使用正则表达式实现白名单。为什么要这样做
echo$hint==“”?“没有建议”:$hint代码>我的意思是分配??“txtHint”是一个id或一个类?或两者都是?这不是分配?对此有评论。但我还是要解释一下,就像这样:echo检查($hint==“”)是否为真,它是真的,它将回显“无建议”,否则“如果它不是空的,它将回显$hint”。@Lambda7“txthint”是一个id。然后你应该在$hint='。$name.
';和第二个中更正它
<?php
$a[] = "Anna";
$a[] = "Brittany";
$a[] = "Cinderella";
$q = test_input($_REQUEST["q"]);
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$hint = "";
// lookup all hints from array if $q is different from ""
if ($q !== "") {
$q = strtolower($q);
$len=strlen($q);
foreach($a as $name) {
if (stristr($q, substr($name, 0, $len))) {
if ($hint === "") {
$hint = '<p class="txtHint">' . $name . '</p>';
} else {
$hint .= '<p class="txtHint">' . $name . '</p>';
}
}
}
}
// Output "no suggestion" if no hint was found or output correct values
echo $hint === "" ? "no suggestion" : $hint;
?>