1

我试图用 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 的较长代码,可在此处获得。为了以防万一,我还粘贴了 SQL 数据,这里是.

提前感谢您的任何想法!

4

0 回答 0