我最近读了一篇文章,说你永远不应该相信用户发送数据。
因此,假设我有以下表单输入:
<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" />
是否可以检查发布的答案是否在带有答案的数组中:
$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']
它就足够了?