我要运行确认安全问题的小程序:
$questions = array('dog legs','car legs','the best country in the world');
$answers = array(4,4,'USA');
$questionId = rand(0, (count($questions)-1));
然后
$ans = $_POST['answer'];
if($ans == $answers[$questionId]){echo 'Confirmed !';}
所以可能不是每次我回答都是正确的,因为在发送表格后,rand 函数运行并更改了问题!有什么解决办法吗?
最佳答案
您可以在表单中传递一个隐藏的输入,并将问题 ID 作为值,这样 ID 将被重新发送。
<input type="hidden" name="questionId" value="<?php echo $questionId; ?>"></input>
然后检查表单是否提交。
<?php
if ($_POST['questionID'])
$questionID = $_POST['questionID'];
else
$questionID = rand(0, (count($questions)-1));
您还可以使用 base64 编码来保护所有这些
关于php - 为什么我每次刷新页面都使用 rand()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16614572/