Reputation: 28505
I know that this post has many duplicates but none of these provide an explanatory answer, more a bunch of code and that's it. I'd like to better understand what's going on.
I have a sequence of pages where users fill-in information on each page. However, I want them to be able to go back to a previous page to edit details - without getting a Document Expired / Cache Missing error. I know that this is caused by POST, because the server needs that post information, so it needs to be resend. But I don't understand the given solutions.
The answer linked above proposes this:
if ('GET' == $_SERVER['REQUEST_METHOD']) {
//display view
}
else if ('POST' == $_SERVER['REQUEST_METHOD']) {
//process input
//update session
header('Location: /next/page/to/view', true, 303);
}
But what does this do? I've been reading into GET and POST and still don't understand what this does, nor how I should use it. My current pages use SESSIONs, e.g. on page 2, after form submission on page 1:
if (isset($_POST['input'])) {
$input = $_POST['input'];
$_SESSION['example'] = $input;
}
and then on page 3:
if (isset($_POST['otherinput'])) {
$otherinput = $_POST['otherinput'];
$_SESSION['otherexample'] = $otherinput ;
}
if (isset($_SESSION['example'])) {
$input = $_SESSION['example'];
}
But as said this won't work when going back, without the user needing to refresh the page. How do I use the proposed solution, and please also explain what is going on. I am here to learn, but unfortunately many answers just give some code and that's it. I also like an explanation!
Edit. I also found that this works, but also in this case I don't understand why. Further explanation is needed for me. What is the best way?
<?php
session_cache_limiter('private');
session_start();
header('Content-Type:text/html; charset=utf-8');
?>
Upvotes: 0
Views: 287
Reputation: 2641
Martin's comment is quite right, but I will elaborate on it a bit.
Let's consider the following example: Page 1 has a form on it that is submitted via POST to page 2. Page 2 displays the results and a link to page 3. Page 1 also has an link to page 4, which is just empty.
Now let's look what happens, when one clicks the link on page 1.
But what happens, when you submit data on pages 2 and 3?
So, what can you do to display the page without sending data again and triggering the warning? When a browser receives the information, that a site relocated (http Status 301 and 302) it stores the new url in its history instead of the old one. We're going to use this by doing all the stuff we need to do with the posted data and then redirect the browser to a different url. The browser will perform a GET request to get the new page. So, when you click the back button the GET request is recorded in the history of the browser and not the POST request. So there is no warning issued.
I hope it's understandable ;)
Upvotes: 1