可以重置cookie的原因是什么?

时间:2016-09-16 22:30:56

标签: php cookies

我正在尝试检查用户之前是否参加过测验,如果他/她有,我希望他们无法再次参加测验。我使用cookie实现了代码来执行此操作,由于某种原因,我的代码拒绝阻止用户再次参加测验。我现在已经盯着这个很长一段时间了,所以帮助会很好!

注意:$ _SESSION [" index"]最初从前一页设置为0,$ _COOKIE [' quizTakers"]最初是一个空数组。每个问题一次出现一个。

   <?php 
        session_start();
        #get array of quizTakers from cookie
        $addUser = unserialize($_COOKIE['quizTakers']);
        $userN = $_SESSION['username']; 

        #check if user has taken quiz already and make sure you only check once and not after every question submit
        if(count($addUser) != 0 && intval($_SESSION["index"]) == 0 ){
            foreach ($addUser as $user) {
                if( strcmp($userN,$user) ){
                    echo "You already took the quiz! <br \>";
                    echo "<form action=\"changeUser.php\" method=\"post\"> Go Back: <input type=\"submit\"><br \> </form>";
                    exit();
                }
            }
            array_push($addUser, $userN);
            setcookie('quizTakers', serialize($addUser), time()+86400); 
            echo "loop was entered <br />";
        } 
        #if array is empty(this is should execute the every first time someone takes the quiz
        elseif (count($addUser) == 0) { 
            #add user to array if this is first person taking a quiz yo
            array_push($addUser, $userN);
            setcookie('quizTakers', serialize($addUser), time()+86400); 
            echo "cookie added  line 29 <br/>";
        }

        $indexTemp = intVal($_SESSION["index"]);

        if(isset($_SESSION["notFirstIndex"])){
            #get array of correct answers
            $correctAns = $_SESSION["correctAnswers"];
            #get particular answer at current index
            $currentCorrectAns = intval($correctAns[$indexTemp]) +1;


            $userAns = intval($_POST['ans']);
            echo "The User picked: ".$userAns." and the correct Answer was: ".$currentCorrectAns."<br/>";

            if($userAns == intVal($currentCorrectAns)){
                echo " you were correct! <br />";
                $_SESSION["totalCorrect"] += 1;
            }

            else{
                echo "you were wrong";
                $_SESSION["totalIncorrect"] +=1;
            }

        }
        elseif(!isset($_SESSION['notFirstIndex'])){
            echo "Welcome to your quiz, $userN <br />";
            echo "You havent answered any questions yet! <br />";

        }

    ?>
    <!DOCTYPE html>
    <html> 
    <HR>
    </html>

    <?php


        #When questions are over show results
        if($_SESSION["numQuestions"] == $indexTemp){
            $_SESSION["index"] = 0;
            echo "Your Results are: <br /> ";
            echo "Total Questions: ".$_SESSION["numQuestions"]."<br/>";
            echo "Total Correct: ".$_SESSION["totalCorrect"]."<br/>";
            echo "Total Incorrect: ".$_SESSION["totalIncorrect"]."<br/>";
            $percentage = (intval($_SESSION["totalCorrect"]) / intval($_SESSION["numQuestions"])) * 100 ;
            echo "Percentage Rightht: $percentage % <br/ >";
            echo "<form action=\"process.php\" method=\"post\"> Back to Main screen: <input type=\"submit\"><br \> </form>";

            $takers = unserialize($_COOKIE['quizTakers']);
            echo $takers[0];
            if(count($takers) == 1){
                echo "<br />";
                echo "You were the first Quiz Taker: <br />";
                echo "Total Takers: 1 <br />";
                echo "Number Right: ".$_SESSION["totalCorrect"]."<br/>";
                echo "Number Incorrect: ".$_SESSION["totalIncorrect"]."<br/>";
                echo "Average: $percentage % <br/ >";
                exit();
            }

            exit();
        }

        $filename = $_SESSION["quizOfTheDay"];

        $quizStuff = file($filename);
        $ctr =1;


        $questionInfo = $quizStuff[$indexTemp];

        $questionParse = explode("#", $questionInfo);
        #$_SESSION["correctAns"] = $questionParse[2];
        #echo $_SESSION["correctAns"]." from line 56 <br />";
        $_SESSION['notFirstIndex'] = "true";
        $answerChoices = explode(":",$questionParse[1]);

        echo "$questionParse[0]? <br />";
        ?>

        <!DOCTYPE html>
        <html>
        <form action="questions.php" method="post">
            <?php
                foreach ($answerChoices as $answerChoice) {
                    echo "<input type='radio' name='ans' id='q1' value=".$ctr."> <label for='q1'>".$answerChoice."</label> <br />";
                    $ctr +=1;   
                }   
                $_SESSION["index"] = $indexTemp +1;
            ?>
        <input type="submit" name="submit" value="GO!">
        </form>

        </html>

3 个答案:

答案 0 :(得分:0)

我还没有看过你的代码,但是你是否真的理解了cookie / session的概念?

Cookie会在浏览器中存储数据,并在每次请求网站时发送数据。会话将把数据存储在服务器上,会在客户端存储cookie,以便在后续请求中识别用户。

因此,用户可以简单地擦除cookie(在这两种情况下)并且服务器不知道该用户曾经参加过测验。

您可以尝试的是IP地址和浏览器指标的组合,但要注意IP地址可以更改(非常快速;))

答案 1 :(得分:0)

Cookie可以被用户删除,你不知道他们是否做了你的测验。

如果你想要一个永久的方式来知道他们是否完成了你的测验,你可能需要强迫人们注册才能获得你的测验的访问权。

答案 2 :(得分:0)

在设置Cookie之前,$_COOKIE['quizTakers']不存在,当您致电unserialize()时,您将$addUsers设置为false。然后,当您尝试执行array_push($addUser, $userN);时,由于$addUser不是数组,因此失败,因此$addUser仍为false。然后你把它放入cookie中。

下次用户运行脚本时,您会从Cookie中读取false,并且在此处找不到用户名,因此您不会阻止用户再次参加测验。

你使这变得比它需要的复杂得多。您不需要将数组放入cookie中,因为所有用户都不共享cookie。只需将cookie设置为一个简单的字符串并测试cookie是否已设置。

if (isset($_COOKIE['took_quiz'])) {
    echo "You already took the quiz! <br \>";
    echo "<form action=\"changeUser.php\" method=\"post\"> Go Back: <input type=\"submit\"><br \> </form>";
    exit();
}
setcookie('took_quiz', 'true', time()+86400);

正如其他人指出的那样,用户可以通过清除cookie来解决这个问题。因此,如果您需要更安全的东西,您需要实施登录系统并使用数据库或文件来跟踪哪些用户已经参加了测验。