玩会话和POST(会话不会超过2?)

时间:2013-05-07 18:51:07

标签: php session

我开始制作一个步骤系统,投票给这个页面,然后下一步。

我测试了会话现在的工作方式,并且每当您点击“继续”时尝试进行此操作,它会在会话编号中添加+ 1:

$_SESSION['vote_id'] = 1;

if (isset($_POST['continue']))
{
    if ($_SESSION['vote_id'] == 1)
    {
        $_SESSION['vote_id'] = 2;

    }
    else if ($_SESSION['vote_id'] == 2)
    {
        $_SESSION['vote_id'] = 3;
    }
}

但是现在,当我点击继续时,它会变为2,这很不错,但是当我再次点击继续时,它会保持在2?

怎么了?

        <?php
            if (isset($_SESSION['vote_id']))
            {
                if ($_SESSION['vote_id'] == 1)
                {
                    echo '1';
                }
                else if ($_SESSION['vote_id'] == 2)
                {
                    echo '2';
                }
                else if ($_SESSION['vote_id'] == 3)
                {
                    echo '3';
                }                   
            }
        ?>
        <span id="head">Welcome</span><br /><br />
        <span id="paragraph">
        We currently have 7 voting sites. 
        Don't be afraid! We do NOT require you to vote on them all.
        You will receive one point per vote, to finish voting, please click on the
        button "Finish Voting" ad you will receive your points
        </span><br /><br />
        <form action="index.php" method="post">
        <span id="head">Voting site 1: Runelocus</span><br /><br />
        <div class="button" name="runelocus">Runelocus Vote</div><br /><br />
        <input type="submit" class="button_green" id="right" name="continue" value="Continue">
        </form>
        <?php
            echo $_SESSION['vote_id']; 
        ?>

感谢。

1 个答案:

答案 0 :(得分:1)

在任何html代码session_start()之前启动会话。
你需要这样的东西吗?

    <?php
    session_start();
        if (isset($_SESSION['vote_id']))
        {
            $_SESSION['vote_id'] +=1;                 
        }
        else
        {
            $_SESSION['vote_id'] =0;
        }
    ?>
    <span id="head">Welcome</span><br /><br />
    <span id="paragraph">
    We currently have 7 voting sites. 
    Don't be afraid! We do NOT require you to vote on them all.
    You will receive one point per vote, to finish voting, please click on the
    button "Finish Voting" ad you will receive your points
    </span><br /><br />
    <form action="index.php" method="post">
    <span id="head">Voting site 1: Runelocus</span><br /><br />
    <div class="button" name="runelocus">Runelocus Vote</div><br /><br />
    <input type="submit" class="button_green" id="right" name="continue" value="Continue">
    </form>
    <?php
        echo $_SESSION['vote_id']; 
    ?>