我试图在PHP中制作一个“简单”的猜谜游戏。然而,似乎当用户按下“提交”按钮来提交答案时,在某些(看似随机的)时刻,而不是再次看到问题并看到正确的答案,则显示不同的问题和答案。我有一种感觉,问题出在逻辑中,或者存储问题编号的cookie。这是一个剪辑:
if (isset($_POST['q'])) {
$questionNo = $_POST['q'];
$text = str_replace("\\", "", $_POST['ans']);
$text = ltrim(rtrim($text, "'"), "'");
$button = "Next one";
$action = "window.location='game.php';";
$verify = True;
} else {
$verify = False;
$answered = unserialize(urldecode($_COOKIE['answered']));
if (gettype($answered) == 'boolean') {
$answered[0] = "";
}
while (True) {
if (array_search($questionNo, $answered) > 0 && count($answered) < ($numberOfQuestions + 1)) {
$questionNo = $questionNo + 1;
$questionNo = ($questionNo > $numberOfQuestions ? $questionNo - $numberOfQuestions : $questionNo);
} else {
$answered[count($answered)] = $questionNo;
setcookie('answered', urlencode(serialize($answered)));
break;
}
}
我为PasteBin添加的代码越长,available here。我以防万一也粘贴了SQL数据which is here。
感谢任何想法!