PHP page refresh issue

I am working on an online survey.
Each time I clicked the “Next Question” button, I tried the following:

if(isset($_POST['submit']) && isset($_POST['ans']))
{
  //process answers and fetch next question
  //Increment the Session variable which contains serial of next question
  $_SESSION['qnum'] = $_SESSION['qnum']+1;
}

Whenever I press the F5 button, the session is updated and a new question is called up.
The session should only increase when you click the button for the next question, otherwise it should not increase.
Below is what I tried after linking to an article:

$pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';
if($pageWasRefreshed) 
{
  //refetch the current question.
}
else
{
 //refetch the current question and increment the Session Variable 
}
  • This approach worked fine in Mozilla Firefox
  • In IE, both button presses and F5 are deleted as not updated
  • In Chrome, both buttons and F5 are accepted as updated

I tried other options from other articles on StackOverflow and Google, but they did not work at all , including the ajax based solution .

+4
3

:

$RSig = md5($_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING'].print_r($_POST, true));

    if(isset($_SESSION['LastRequest']) && $_SESSION['LastRequest'] == $RSig)
    {
        $_SESSION['reqstat']='refresh';
    }
    else
    {
        $_SESSION['qnum'] = $_SESSION['qnum']+1;
        $_SESSION['reqstat']='newrequest';
        $_SESSION['LastRequest'] = $RSig;
    }

Session: reqstat , , .

+1

, POST.

, POST, php.

:

<?php
if($_SERVER['REQUEST_METHOD'] === 'POST') {
  //handle the POST data
  ...

  // now refresh
  header('location: index.php');  // or what ever url you were on
  exit();
}
// else: show the page
echo '<html> ...';
?>

: ('location:...'), /, / /...

, .php "<" ( php-)

+1

,

if(isset($_POST['submit']) && isset($_POST['ans']))
{
  //I am assuming we have question id or any  uniq data about question
  if($_SESSION['last_que'] != $_POST['que_id']){
      $_SESSION['qnum'] = $_SESSION['qnum']+1;
      $_SESSION['last_que'] = $_POST['que_id'];
  }

}

, arrays array_search

0

Source: https://habr.com/ru/post/1662629/


All Articles