Php测验网站没有携带会话变量

Php测验网站没有携带会话变量,php,xml,session,Php,Xml,Session,我有一个php文件和一个xml文件。php应该将xml解析为测验,但每当我单击submit query以输入答案选项时,会话变量不会继续。该页面应该刷新并为测验提供另一个问题。 这是php的正确用法吗 <!DOCTYPE html> <?php session_start(); $_SESSION['qcorrect']; $_SESSION['quizno']; $_SESSION['qvar'] = 0; if (!isset($_SESSI

我有一个php文件和一个xml文件。php应该将xml解析为测验,但每当我单击submit query以输入答案选项时,会话变量不会继续。该页面应该刷新并为测验提供另一个问题。 这是php的正确用法吗

       <!DOCTYPE html>
<?php

    session_start(); $_SESSION['qcorrect']; $_SESSION['quizno']; $_SESSION['qvar'] = 0;
    if (!isset($_SESSION['quizno'])){
        $_SESSION['quizno'] = 0;

    }
    if (!isset($_SESSION['qcorrect'])){
        $_SESSION['qcorrect'] = 0;
    }

    ?>
    <html>

    <head>
    <style>
    h1 { text-align:center; }
    p  { text-align:center; }

    </style>

     <title> Memequiz.net </title>

    </head>

        <body>

        <h1>Memequiz.net</h1>



        <div id = "image"  style = width:"500px" align = "middle">

    <?php   


            $xml = simplexml_load_file('firsquiz.xml');


            $question = ($xml -> question[$_SESSION['quizno']] -> ask);
            $correctans = ($xml -> question[$_SESSION['quizno']] -> correctanswer);
            $answer0 = ($xml -> question[$_SESSION['quizno']] -> answers -> answer[$_SESSION['qvar']] -> text);
            $answer1 = ($xml -> question[$_SESSION['quizno']] -> answers -> answer[$_SESSION['qvar']+1] -> text);
            $answer2 = ($xml -> question[$_SESSION['quizno']] -> answers -> answer[$_SESSION['qvar']+2] -> text);
            $answer3 = ($xml -> question[$_SESSION['quizno']] -> answers -> answer[$_SESSION['qvar']+3] -> text);

            echo "<img src = \"".$xml -> question[$_SESSION['quizno']] -> picture."\" alt = \"Meme\" width = \"400\" height = \"350\">";
            echo "<p><br>Question ".$_SESSION['quizno']."<p>Number Correct: ".$_SESSION['qcorrect']."</p><p>".$question."<br></p>";




            echo "<form action = \"\" method = \"get\">";

            echo "<table>";

            echo "<tr> <td> <input type = \"radio\" name = \"ans\" value = \"".$answer0."\"></td> <td>".$answer0."</td> </tr>";
            echo "<tr> <td> <input type = \"radio\" name = \"ans\" value = \"".$answer1."\"></td> <td>".$answer1."</td> </tr>";
            echo "<tr> <td> <input type = \"radio\" name = \"ans\" value = \"".$answer2."\"></td> <td>".$answer2."</td> </tr>";
            echo "<tr> <td> <input type = \"radio\" name = \"ans\" value = \"".$answer3."\"></td> <td>".$answer3."</td> </tr>";

            echo "</table>";

            echo "<input type = \"submit\" name = \"submit1\">";
            echo "<input type = \"submit\" name = \"refresh\">";

            echo "</form>";

            echo "You submitted: ";



            echo "<br><br><br>";
            echo "my xml file: ";


                if (isset($_GET['submit1'])){

                $_SESSION['quizno']++;

                if ($correctans == $_GET['ans']){ 

                    echo "Correct Answer!";
                    $_SESSION['qcorrect']++;


                }else{
                    echo "The wrong answer, try again!";

                }

                }

                if (isset($_GET['refresh'])){
                    $_SESSION['qcorrect'] = 0;
                     $_SESSION['quizno'] = 0;
                }









        ?>  



        </div>
    </body>

</html>




 XML file:

    <?xml version = "1.0"?>
    <quiz>
    <question>
        <ask>What is the name of the movie that this meme comes from?</ask>
        <correctanswer>Downfall</correctanswer>
        <picture>question1.jpg</picture>
        <answers>
            <answer>
                <text>Stalingrad</text>
            </answer>

            <answer>
                <text>Downfall</text>
            </answer>

            <answer>
                <text>Band of Brothers</text>
            </answer>

            <answer>
                <text>The Fuhrer</text>

            </answer>
        </answers>
    </question>

    <question>
        <ask>What is the name of this actor?</ask>
        <correctanswer>Bruno Ganz</correctanswer>
        <picture>question1.jpg</picture>
        <answers>
            <answer>
                <text>Bruno Ganz</text>
            </answer>
            <answer>
                <text>Ulrich Matthes</text>
            </answer>
            <answer>
                <text>Thomas Kretschmann</text>
            </answer>
            <answer>
                <text>Hans Funck</text>
            </answer>
        </answers>

    </question>

    <question>
        <ask>What is the name of the youtube account in which this first appeared in?</ask>
        <picture>questionn2.jpg</picture>
        <correctanswer>wzr0713</correctanswer>
        <answers>
        <answer>
            <text>wzr0713</text>
        </answer>
        <answer>
            <text>rerer434</text>
        </answer>
        <answer>
            <text>karetoshare</text>
        </answer>
        <answer>
            <text>blueberrypie573</text>
        </answer>

        </answers>

    </question>

    <question>
        <ask>What date did this picture first appear on the internet?</ask>
        <picture>questionn2.jpg</picture>
        <correctanswer>June 6 2012</correctanswer>
        <answers>
            <answer>
                <text>Aug 12 2011</text>
            </answer>
            <answer>
                <text>Oct 22 2010</text>
            </answer>
            <answer>
                <text>Aug 13 2011</text>
            </answer>
            <answer>
                <text>June 6 2012</text>
            </answer>
        </answers>
    </question>

    <question>
        <ask>This is a stock photo from which company?</ask>
        <correctanswer>Corbis Images</correctanswer>
        <picture>question3.png</picture>

        <answers>
            <answer>
                <text>Foter</text>
            </answer>
            <answer>
                <text>Getty Images</text>
            </answer>
            <answer>
                <text>Sipa Press</text>
            </answer>
            <answer>
                <text>Corbis Images</text>
            </answer>
        </answers>
    </question>

    <question>
        <ask>Which website did this meme originate from?</ask>
        <correctanswer>quickmeme</correctanswer>
        <picture>question3.png</picture>
        <answers>
            <answer>
                <text>memebase</text>
            </answer>
            <answer>
                <text>funnyjunk</text>
            </answer>
            <answer>
                <text>4chan</text>
            </answer>
            <answer>
                <text>quickmeme</text>
            </answer>
        </answers>
    </question>

    <question>
        <ask>What is the name of the comedy show in which this image first appeared?</ask>
        <correctanswer>Panic Face King</correctanswer>
        <picture>question4.jpeg</picture>
        <answers>
            <answer>
                <text>Takeshi Castle</text>
            </answer>
            <answer>
                <text>Panic Face King</text>
            </answer>
            <answer>
                <text>Spank the laugh</text>
            </answer>
            <answer>
                <text>The Big Moment</text>
            </answer>
        </answers>
    </question>

    <question>
        <ask>What is the name of the person above?</ask>
        <correctanswer>Ayumu Kato</correctanswer>
        <picture>question4.jpeg</picture>
        <answers>
            <answer>
                <text>Katashi Samosoro</text>
            </answer>
            <answer>
                <text>Konno Miko</text>
            </answer>
            <answer>
                <text>Ayumu Kato</text>
            </answer>
            <answer>
                <text>Nakamura Asuka</text>
            </answer>
        </answers>
    </question>


</quiz>

h1{文本对齐:居中;}
p{文本对齐:居中;}
Memequiz.net
Memequiz.net

你认为是什么$_会议['quizno']正在做什么?因为它实际上什么都不做。这不是创建会话变量的地方吗?我想创建一些变量,这些变量可以在用户刷新页面时进行转换。我应该将其存储在cookie中吗?您应该在
$\u会话
数组中创建新标记,方法是为它们赋值,而不是声明它们。如果这是按照您认为的方式进行的,并且只是声明
$\u SESSION['quizno']
以某种方式在
$\u SESSIONS
中定义了索引,您的
if(!isset($\u SESSION['quizno'])
测试将永远无法工作。好的。我懂了。我删除了那两行。问题是,每当我单击“提交”按钮时,它都会将我输入单选按钮的值与下一个问题(而不是当前问题)进行比较。无论问题是否正确,你都会增加
quizno
,这意味着每次提交表单时,你都会得到下一个问题。