返回:文档过期/缓存丢失

问题描述:

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');
?>

我知道这篇文章有许多重复但这些都没有提供解释性答案,更多的是一堆代码,就是这样。 我想更好地了解正在发生的事情。 p>

我有一系列页面,用户在每个页面上填写信息。 但是,我希望他们能够返回上一页编辑详细信息 - 不会出现文档过期/缓存丢失错误。 我知道这是由POST造成的,因为服务器需要发布信息,因此需要重新发送。 但我不明白给定的解决方案。 p>

上面提到的答案提出了这个问题: p>

  if('GET'== $  _SERVER ['REQUEST_METHOD']){
 //显示视图
} 
else if('POST'== $ _SERVER ['REQUEST_METHOD']){
 //进程输入
 //更新会话
标题 ('位置:/ next / page / to / view',true,303); 
} 
  code>  pre> 
 
 

但这是做什么用的? 我一直在阅读GET和POST,但仍然不明白这是什么,也不知道我应该如何使用它。 我当前的页面使用SESSION,例如 在第2页上,在第1页上提交表单后: p>

  if(isset($ _ POST ['input'])){
 $ input = $ _POST ['input'  ]; 
 $ _SESSION ['example'] = $ input; 
} 
  code>  pre> 
 
 

然后在第3页: p> if(isset($ _ POST ['otherinput'])){ $ otherinput = $ _POST ['otherinput']; $ _SESSION ['otherexample'] = $ otherinput; } if (isset($ _ SESSION ['example'])){ $ input = $ _SESSION ['example']; } code> pre>

但是如上所述 在没有用户需要刷新页面的情况下返回时将无法工作。 如何使用建议的解决方案,还请解释 em>正在发生的事情。 我在这里学习,但不幸的是,许多答案只是提供一些代码,就是这样。 我也想解释一下! p>


编辑。 我也发现这个有效,但在这种情况下我也不明白为什么。 我需要进一步解释。 什么是最好的方法? p>

 &lt;?php 
session_cache_limiter('private'); 
session_start(); 
header('Content-Type:text / html; charset  = utf-8'); 
?&gt; 
  code>  pre> 
  div>

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.

  • Browser displays page 1. He got it by using a GET request.
  • You click the link and the browser performs another GET request for page 4
  • If you click the back button the browser looks at the last page in its history. It got it using a GET request, so it assumes it has to be static data. No problem going back, everything is fine.

But what happens, when you submit data on pages 2 and 3?

  • Browser displays page 1
  • You enter data in the form and submit it via POST
  • Page 2 is displayed and you click the link to retrieve page 3
  • Now when clicking the back button the browser looks at its history again. It displayed page 2 but it obtained it via POST. This means you have taken an action which probably changed somehting. Page 2 must be dynamic, so it can only be restored by sending the exact same data again. Sending data without the user actively wanting to do so is not considered a good practice, so the browser asks the user if it should send the data again. The question is different in all browsers. In some it is more like "send again?" and others are more like "naa, you don't want to do this again. The action has expired for sure. Trust me.".

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 ;)