-1

I would like to prevent the session to increment if the page gets refreshed. I tried to check if POST was already set, like this if (isset($_POST)){$_SESSION['page']++;}, but it didn't solve the problem, because the form has to be sent more than once.

I would like $_SESSION['page']++ to increment only when I submit data from the form, not when the page gets refreshed.

EDIT: I posted the whole code as it wasn't clear how it works.

IMPORTANT: Apparently, the POST/REDIRECT/GET method won't work, as I don't need to redirect the user to another page after the data gets sent. Instead I need to reload the same page. Don't take my word for granted.

//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>  ```
  • Why don't you just add the page number in the form. – Dhaval Purohit Mar 02 '20 at 09:07
  • Use this: `if (!empty($_POST)){$_SESSION['page']++;}` to check if form has been submitted! – Serhii Puchkovskyi Mar 02 '20 at 09:07
  • @SerhiiPuchkovskyi when user press F5 then it will re-submit the form and condition will not run as expected. – Dhaval Purohit Mar 02 '20 at 09:09
  • @DhavalPurohit You mean in "action" ? I have to increment the session at least 5 times on the same page. Not sure if this answers your question. – CortoMaltese Mar 02 '20 at 09:50
  • Does this answer your question? [How to prevent form resubmission when page is refreshed (F5 / CTRL+R)](https://stackoverflow.com/questions/6320113/how-to-prevent-form-resubmission-when-page-is-refreshed-f5-ctrlr) – CBroe Mar 02 '20 at 09:50
  • `isset($_POST)` is completely pointless to use as criterion here. First of all, `$_POST` is _always_ set, even if the request was not even a POST request - it might be _empty_, but that is a different thing. And even that aside, refreshing the result page of a POST form submission, _will_ just send the same data again (if the user confirms, and doesn’t cancel). You will have to apply something like the POST/REDIRECT/GET pattern first of all here, if you want to prevent this from happening. – CBroe Mar 02 '20 at 09:52
  • @CortoMaltese I am talking that why dont you just submit page number as an input and then on every submit just increment the previous value. – Dhaval Purohit Mar 02 '20 at 09:57
  • Did you get it @CortoMaltese? – Dhaval Purohit Mar 02 '20 at 09:58
  • @DhavalPurohit Not really... – CortoMaltese Mar 02 '20 at 09:59
  • @CBroe It doesn't. – CortoMaltese Mar 02 '20 at 10:01
  • Then explain _why_ it doesn’t. POST/REDIRECT/GET _is_ the most common way to deal with such issues. If you say this doesn’t help you here - then you need to explain, why not. Or give us a better explanation of what you are actually trying to _achieve_ here in the first place. What exactly do you want to count here, based on what and when? – CBroe Mar 02 '20 at 10:03
  • @cBroe I tried to use ```(isset($_POST['answer']) && $_POST['randcheck']==$_SESSION['rand'])``` as showed in the page you linked above, but it didn't work. The problem is that the user will be able to refresh the page after the session is been incremented the first time. In my page the session has to be incremented at least 5 times. Do you need further information? – CortoMaltese Mar 02 '20 at 10:18
  • @DhavalPurohit Could you provide me with an example? I'd like to understand your advice better. – CortoMaltese Mar 02 '20 at 10:23
  • You can not achieve this by checking on any of the submitted parameters - because those will be exactly the same again, after the refresh. The mentioned POST/REDIRECT/GET pattern is one way to work around this issue. If you don’t like that - then you will have to implement some sort of form token mechanism, that allows you to check if a set of data has already been processed, so that you can reject it or skip processing it again. P/R/G would be the much simpler approach though. – CBroe Mar 02 '20 at 11:34
  • @CBroe Looks like POST/REDIRECT/GET is used to redirect the user to another page. In my case I first need to send the data to the same page and then reload it. – CortoMaltese Mar 02 '20 at 16:12
  • @CortoMaltese when you just post the first form in that form use one hidden param as eg. form_count =1 and then on every submit just add +1 of previous submitted form. That's way even if you refresh the sending value would be same as the previous one. and incremental value would be the same. – Dhaval Purohit Mar 03 '20 at 05:07

1 Answers1

0

Below is the POC what i am trying to say you on the comments. Hope you get it. I have added one input parameter on the form so whenever that particular form submitted then and only then it will get incremented otherwise not.

<!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>
Dhaval Purohit
  • 1,270
  • 10
  • 28
  • 1
    Your example doesn't work. It will prevent the page from incrementing the very first time it gets loaded, but starting from page 2 the problem will recur. I solved it myself though, I used hidden input as you suggested. I will edit my question to explain how I solved it. Thanks for your help, really aprecciate it. – CortoMaltese Mar 06 '20 at 15:00