我最近看過一篇文章說,你永遠不應該信任發送數據中的用戶。檢查提交值是否在數組中?
所以我們可以說,我有如下形式輸入:
<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']
它就夠了?