php 返回按钮重新提交表单数据($_POST)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2146431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Back button re-submit form data ($_POST)
提问by Cudos
My problem is that the back button causes the browser to say something like "Page expired" when the previous page was created by a form.
我的问题是当上一页是由表单创建时,后退按钮会导致浏览器说“页面已过期”。
Example:
例子:
- page1: form submitted with search criterias ($_POST request, form points to page2)
- page2: Receives $_POST request and show result (list of user with links, points to page3)
- page3: Show user profile
- page1:使用搜索条件提交的表单($_POST 请求,表单指向 page2)
- page2:接收$_POST请求并显示结果(有链接的用户列表,指向page3)
- page3:显示用户资料
Now when the visitor clicks the back button in the browser it will show something like "Page expired".
现在,当访问者单击浏览器中的后退按钮时,它将显示类似“页面已过期”的内容。
Instead the previous page should be shown with no warnings (page2, with the userlist)
相反,应显示上一页而没有警告(第 2 页,带有用户列表)
How are your strategies to get around this behavior?
您如何应对这种行为?
回答by Pascal MARTIN
If you are submitting a for with search parameters, you are trying to getsome data, not modify some.
如果您提交带有搜索参数的 for,您是在尝试获取一些数据,而不是修改一些数据。
So, you should use the HTTP GET method, and not POST : POST should be used when you intend to create/modify data, and GET should be used when you intend to fetch some data.
因此,您应该使用 HTTP GET 方法,而不是 POST :当您打算创建/修改数据时应该使用 POST,而当您打算获取一些数据时应该使用 GET。
Or, if you have some create/modify operation that has to be done :
或者,如果您有一些必须完成的创建/修改操作:
- The form first POSTs to a first page
- That page does some operations (like writing something to a database)
- And then redirects to another page, using a
LocationHTTP header.
- It's that last page, that's queries by the browser using a GET requests, that displays the data fetched from the parameters received in the URL.
- 表单首先发布到第一页
- 该页面执行一些操作(例如向数据库写入内容)
- 然后使用
LocationHTTP 标头重定向到另一个页面。
- 最后一页是浏览器使用 GET 请求进行的查询,它显示从 URL 中接收的参数中获取的数据。
See the Post/Redirect/Getpage on wikipedia, about this.
有关此内容,请参阅维基百科上的Post/Redirect/Get页面。
回答by Alix Axel
回答by Ignacio Vazquez-Abrams
Send a Locationheader in the script you POSTed to, pointing to the page that comes after.
Location在您发布到的脚本中发送一个标题,指向后面的页面。
回答by Pim Jager
Don't use POST for search. Search can safely be done with GET since it won't alter anything.
不要使用 POST 进行搜索。使用 GET 可以安全地完成搜索,因为它不会改变任何东西。
回答by Chung Lau
This applies to PHP and IE8.
这适用于 PHP 和 IE8。
Not only must you set cacheing to private, but you must remove the 4 cacheing headers and this can only be done with PHP 5.3. In PHP 5.2 you can only set the 4 headers to blank values if using the Zend Framework's setHeader() method. For some reason is not sufficient on IE8 to set the 4 header values to empty values. Here's the code for PHP 5.3:
您不仅必须将缓存设置为私有,还必须删除 4 个缓存标头,而这只能在 PHP 5.3 中完成。在 PHP 5.2 中,如果使用 Zend Framework 的 setHeader() 方法,您只能将 4 个标头设置为空值。出于某种原因,在 IE8 上不足以将 4 个标头值设置为空值。这是 PHP 5.3 的代码:
header_remove("Expires");
header_remove("Cache-Control");
header_remove("Pragma");
header_remove("Last-Modified");
回答by Chung Lau
You can use session to do this.
您可以使用会话来执行此操作。
eg.
例如。
$_SESSION['name'] = $_POST['name'];
$_SESSION['name'] = $_POST['name'];
Remeber to unset your variables after the process is completed to optimize memory usage.
请记住在该过程完成后取消设置您的变量以优化内存使用。


