19

So here is the deal, I am using HTML forms to transfer variables from page to page and PHP script to create pages based on values submitted. In general it looks like this: from the catalog of items you select what you want and the next page shows details for this specific item. Everything works perfect, except one thing:

Whenever I use browser's back button, I always get the error: ERR_CACHE_MISS and I need to refresh page and then confirm that I really want to resubmit data.

Is there any way to fix this, so my customers would be able just to use back button as they supposed to.

Here is the full text that browser provides me:

This webpage requires data that you entered earlier in order to be properly displayed. You can send this data again, but by doing so you will repeat any action this page previously performed. Reload this webpage. Press the reload button to resubmit the data needed to load the page. Error code: ERR_CACHE_MISS

6
  • 2
    Are you open to using a server end data storage? MySQL, Redis? The best way is to store the data on the server as the user navigates around. Oct 7, 2013 at 0:53
  • and by this you mean I need to store all values from submitted forms during user's session? Can I use session storage for that? Oct 7, 2013 at 0:56
  • You can use session storage, but it is not very scale-able and becomes the wrong way to go if the amount of data every goes from a little bit to more than a little bit. I would say for homework session storage is fine, for production it's not enough. Oct 7, 2013 at 1:02
  • well, I do have very small project with just 2 levels for navigation (catalog and item details), so I need to store only 2-3 links for going back to catalog, so I guess it could work for me. what is the way I can connect back button and sessionStorage? Oct 7, 2013 at 1:06
  • Why wouldn't he be able to use sessions? They are used widely for shopping carts when not logged in (same goes for cookies), for storing search queries, etc. So yes, in my opinion sessions are a good way to catch this. 1) Submit form, 2) Store form data in session, 3) Redirect to the page performing the action based on the session, 4) Enjoy. If a user presses back, he/she goes to the page using the sessions instead of the post data Nov 9, 2013 at 7:55

6 Answers 6

32

When you post forms with php, or any other data, you may come back to the page and find a message in the browser like "Document Expired" or "Confirm Form Resubmission With Chrome". These messages are a safety precaution the browser uses with sensitive data such as post variables. The browser will not automatically give you the fresh page again. You must reload the page by clicking try again or with a page refresh. Then, it operates as you would expect it to.

However, the php coder can work around the annoying message from the browser by adding a little code into the script. The example shows a couple of lines of code that can be added above session_start() in order to be able to go back and forth to the page when you post without any hangups.The 'private_no_expire' mode means that the client will not receive the expired header in the first place.

header('Cache-Control: no cache'); //no cache
session_cache_limiter('private_no_expire'); // works
//session_cache_limiter('public'); // works too
session_start();
1
  • This doesn't work for me and I have exactly the same scenario, has this changed? Jul 24, 2019 at 8:55
9

**Some background: Credit goes to bruce (sqlwork.com) for his excellent explanation.

This web page requires data that you entered earlier in order to be properly displayed. You can send this data again, but by doing so you will repeat any action this page previously performed. Press Reload to resend that data and display this page.

Because of the sloppy coding practices of web developers browsers were forced to add this message. the scenario is as follows:

1) user fills in form and submits (posts form) 2) the server process the post data and responds with a new page (confirm) marked as not cacheable 3) the user navigates to a new page. 4) the user press back:

for the the browser to display the page in step 2, because its marked no-cache, it must request it from the server, in other words do the repost of the data (do step 1). here is were the sloppy coding came in, if this was an credit card charge, and repost detection was not on the server, the card is charged twice. this was (is) so common a problem, that the browsers had to detect this and warn the users.

the best fix is in step two, the server sends a redirect to the confirm page. then when the user accesses the confirm via history or back, its a get request, not a post request and will not show the warning.

note: webform's postback model lends itself to this problem. also avoid server transfers.

My solution

$_SESSION['home'] used to store any errors on home page.

$_SESSION['tempEmail'] used to echo value on php form.

Note: Use one unique session variable for each page that has a HTML form for error handling and also any session variable for each value that is echoed on HTML form.

<?php
session_start(); 

//Initialize variables not initialized without overwriting previously set variables.
if(!isset($_SESSION['home'])) {
$_SESSION['home']="";
$_SESSION['tempEmail']="";
}

Optional - If logged in, assign email address to the $_SESSION['tempEmail'] variable (if not previously done) to pre-fill HTML form.

if(isset($_POST['Submit'])){

---your code---


    //Error message(s) examples
    $_SESSION['home'] = "Email and Password do not match, please try again.";
    header("Location: " . $_SERVER['REQUEST_URI']);

    $_SESSION['home'] = "Email address format is invalid.  Please recheck.";
    header("Location: " . $_SERVER['REQUEST_URI']);

    //success
    unset ($_SESSION['home']); //optional, unset to clear form values.
    header ("location: nextpage.php"); 
             ---or---
    header("Location: " . $_SERVER['REQUEST_URI']);  //re-post to same page with the $_SESSION['home'] success message.   
}
?>



<body>

Error box

<span><strong class="error"><?php echo $_SESSION['home'] ?></strong></span>

HTML form

<form action="#" name="loginform" method="post" >
    <input type="text" name="userEmail" maxlength="50" title="Enter Your email" autocomplete="off" value="<?php echo htmlspecialchars($_SESSION['tempEmail']); ?>" placeholder="enter email" required/>
    <input type="submit" name="Submit" value="Submit">
</form>

</body>

Not recommended to use on payment page,see discussion above. Tested in Firefox, Chrome, Safari, and IE9. The annoying messages are gone when using back button. Ensure that output buffering is turned "on" in your php script or php.ini to avoid header warnings. You can check your php.ini file for the following;

output_buffering=On
9

I found that using just :

header('Cache-Control: no cache'); //disable validation of form by the browser

resolve the problem

0
6

None of the other answers worked for me.

  • I don't want to redirect
  • Setting different headers didn't work
  • I already use tokens in my post to ensure re-submission can't happen
  • I post to the same url the form is showing on

This simple javascript fixes my issue of the back button throwing "ERR_CACHE_MISS"

if ( window.history.replaceState ) {
  window.history.replaceState( null, null, window.location.href );
}
1
  • 1
    This is really great, I was looking for some JS-based answer to get rid of the annoying "Confirm Form Resubmission" Prompt. For those who might not understand, this adds a history state on the browser so that when the user clicks the "back" button, it goes to the page without the form submission data attached. Oct 25, 2020 at 19:45
5

I tried this answer and it's ok. You have to put this code before: session_start():

session_cache_limiter('private, must-revalidate');
session_cache_expire(60);

Good luck

0
<?php
if(isset($_POST['submit']))
{
 //submission goes here
}
?>

Is this what you were thinking?

edit - SQL really is a beautiful thing to work with, I see it's been added as a recommendation in a comment, and I concur to use SQL if you can, its fast, intuitive and efficient.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.