php – 检查wheter提交值是否在数组中?
作者:互联网
我最近读过一篇文章说,你永远不应该信任用户发送数据.
所以,假设我有以下表单输入:
<label for="input1">Answer 1</label>
<input type="radio" name="question1" value="answer1" id="input1" />
<label for="input2">Answer 2</label>
<input type="radio" name="question1" value="answer2" id="input2" />
<label for="input3">Answer 3</label>
<input type="radio" name="question1" value="answer3" id="input3" />
是否可以检查已发布的答案是否在答案中的aray内:
$question1_answers = array("answer1", "answer2", "answer3");
$answer1 = $_POST['question1'];
if ( in_array($answer1, $question1_answers) ) {
echo "OK!";
} else {
echo "Please select proper answer!";
}
或者也许上面的代码是不必要的,我应该简单地阅读$_POST [‘question1’],这就够了吗?
解决方法:
Is it okay to check whether posted answer is inside aray with answers:
是!这是验证像这样的单选按钮值的非常好的做法.您阅读的文章是正确的,您不应盲目信任用户提供的数据.
标签:php,html-form 来源: https://codeday.me/bug/20190729/1570921.html