【问题标题】:Prevent session to increment if page gets refreshed如果页面被刷新,防止会话增加
【发布时间】:2020-06-14 12:58:19
【问题描述】:

如果页面被刷新,我想阻止会话增加。 我试图检查 POST 是否已经设置,例如 if (isset($_POST)){$_SESSION['page']++;},但它没有解决问题,因为表单必须发送多次。

我希望 $_SESSION['page']++ 仅在我从表单提交数据时递增,而不是在页面刷新时递增。

编辑:我发布了整个代码,因为不清楚它是如何工作的。

重要提示:显然,POST/REDIRECT/GET 方法不起作用,因为我不需要在发送数据后将用户重定向到另一个页面。相反,我需要重新加载同一页面。不要认为我的话是理所当然的。

//Start using session-variables.
session_start();

//Game is started.
if (!isset($_SESSION['page']))
{
    $_SESSION['page']=1;
}
else
{
//Game is on, read answer from previous page.
    $answer=$_POST['answer'];
}

//Figure out what to display.
switch ($_SESSION['page'])
{
    case 1: //First page of the game.
        $picture="pictures/perch.gif";
        $option1="Perch-pike";
        $option2="Pike";
        $option3="Perch";
        $option4="Ruffe";
        break;
    case 2: //Second page of the game.
        $picture="pictures/brownhare.gif";
        $option1="Brown hare";
        $option2="Wolverine";
        $option3="Arctic hare";
        $option4="Badger";
        $_SESSION['answer1']=$answer;
        break;
    case 3: //Third page of the game.
        $picture="pictures/moose.gif";
        $option1="Reindeer";
        $option2="Fallow deer";
        $option3="Roe deer";
        $option4="Moose";
        $_SESSION['answer2']=$answer;
        break;
    case 4: //On last page answers are evaluated.
        $points=0;
        $answer3=$_POST['answer'];

        if ($_SESSION['answer1']==3)
        {
            $points++;
        }

        if ($_SESSION['answer2']==1)
        {
            $points++;
        }

        if ($answer3==4)
        {
            $points++;
        }
        print "
<h2>Your score is $points</h2>";
        print "
<a href='quiz.php'>New game</a>
<br>";
        print "
    <a href='../index.htm'>Back to examples</a>
    <br>";
        session_destroy();
        exit;

}
$_SESSION['page']++;
?>
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
        <html>
            <head>
                <title>Huntersman's examination game</title>
            </head>
            <body>
                <form method="post" action="quiz.php">
                    <center>
                        <h2>What is this?</h2>
                        <table border=1>
                            <tr>
                                <td width="300">
                                    <img src="
                                        <?= $picture?>">
                                    </td>
                                    <td>
                                        <table>
                                            <tr>
                                                <td>
                                                    <?= $option1?>
                                                </td>
                                                <td>
                                                    <input type="radio" name="answer" value="1" checked>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                        <?= $option2?>
                                                    </td>
                                                    <td>
                                                        <input type="radio" name="answer" value="2">
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td>
                                                            <?= $option3?>
                                                        </td>
                                                        <td>
                                                            <input type="radio" name="answer" value="3">
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td>
                                                                <?= $option4?>
                                                            </td>
                                                            <td>
                                                                <input type="radio" name="answer" value="4">
                                                                </td>
                                                            </tr>
                                                            <tr>
                                                                <td>
                                                                </table>
                                                            </td>
                                                        </tr>
                                                    </table>
                                                    <br>
                                                        <input type="submit" value="Answer">
                                                        </center>
                                                    </form>
                                                </body>
                                            </html>  ```

【问题讨论】:

  • 为什么不在表单中添加页码。
  • 使用这个:if (!empty($_POST)){$_SESSION['page']++;} 检查表单是否已提交!
  • @SerhiiPuchkovskyi 当用户按 F5 时,它将重新提交表单并且条件不会按预期运行。
  • @DhavalPurohit 你的意思是“行动”?我必须在同一页面上至少增加 5 次会话。不确定这是否能回答您的问题。

标签: php forms session input refresh


【解决方案1】:

以下是我想在 cmets 上对您说的 POC。希望你能明白。 我在表单上添加了一个输入参数,因此每当提交该特定表单时,它才会增加,否则不会增加。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>Huntersman's examination game</title>
</head>
<?php
//Start using session-variables.
session_start();

//Game is started.
if (!empty($_POST['page']))
{
    $page = $_POST['page']+1;
    $answer=$_POST['answer'];
}
else
{
//Game is on, read answer from previous page.
    $page=1;
}

//Figure out what to display.
switch ($page)
{
    case 1: //First page of the game.
    $picture="pictures/perch.gif";
    $option1="Perch-pike";
    $option2="Pike";
    $option3="Perch";
    $option4="Ruffe";
    break;
    case 2: //Second page of the game.
    $picture="pictures/brownhare.gif";
    $option1="Brown hare";
    $option2="Wolverine";
    $option3="Arctic hare";
    $option4="Badger";
    $_SESSION['answer1']=$answer;
    break;
    case 3: //Third page of the game.
    $picture="pictures/moose.gif";
    $option1="Reindeer";
    $option2="Fallow deer";
    $option3="Roe deer";
    $option4="Moose";
    $_SESSION['answer2']=$answer;
    break;
    case 4: //On last page answers are evaluated.
    $points=0;
    $answer3=$_POST['answer'];

    if ($_SESSION['answer1']==3)
    {
        $points++;
    }

    if ($_SESSION['answer2']==1)
    {
        $points++;
    }

    if ($answer3==4)
    {
        $points++;
    }
    print "
    <h2>Your score is $points</h2>";
    print "
    <a href='quiz.php'>New game</a>
    <br>";
    print "
    <a href='../index.htm'>Back to examples</a>
    <br>";
    session_destroy();
    exit;

}
// $_SESSION['page']++;
?>
<body>
    <form method="post" action="">
        <center>
            <h2>What is this?</h2>
            <table border=1>
                <tr>
                    <td width="300">
                        <img src="
                        <?= $picture?>">
                    </td>
                    <td>
                        <table>
                            <tr>
                                <td>
                                    <?= $option1?>
                                </td>
                                <td>
                                    <input type="radio" name="answer" value="1" checked>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <?= $option2?>
                                </td>
                                <td>
                                    <input type="radio" name="answer" value="2">
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <?= $option3?>
                                </td>
                                <td>
                                    <input type="radio" name="answer" value="3">
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <?= $option4?>
                                </td>
                                <td>
                                    <input type="radio" name="answer" value="4">
                                </td>
                            </tr>
                            <tr>
                                <td>
                                </table>
                            </td>
                        </tr>
                    </table>
                    <br>
                    <input type="hidden" name="page" value="<?php echo $page; ?>">
                    <input type="submit" value="Answer">
                </center>
            </form>
        </body>
        </html>

【讨论】:

  • 您的示例不起作用。它将阻止页面在第一次加载时增加,但从第 2 页开始,问题将再次出现。我自己解决了这个问题,我按照你的建议使用了隐藏输入。我将编辑我的问题以解释我是如何解决它的。感谢您的帮助,非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-25
  • 1970-01-01
  • 2015-08-23
  • 1970-01-01
  • 2012-06-26
  • 1970-01-01
  • 2014-03-22
相关资源
最近更新 更多