php 防止后退按钮显示 POST 确认警报

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/660329/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:24:23  来源:igfitidea点击:

Prevent Back button from showing POST confirmation alert

phpformsposthttp-headers

提问by Milan Babu?kov

I have an application that supplies long list of parameters to a web page, so I have to use POST instead of GET. The problem is that when page gets displayed and user clicks the Back button, Firefox shows up a warning:

我有一个向网页提供一长串参数的应用程序,所以我必须使用 POST 而不是 GET。问题是当页面被显示并且用户点击后退按钮时,Firefox 会显示一个警告:

To display this page, Firefox must send information that will repeat any action (such as a search or order confirmation) that was performed earlier.

要显示此页面,Firefox 必须发送信息来重复之前执行的任何操作(例如搜索或订单确认)。

Since application is built in such way that going Back is a quite common operation, this is really annoying to end users.

由于应用程序的构建方式使得返回是一个非常常见的操作,这对最终用户来说真的很烦人。

Basically, I would like to do it the way this page does:

基本上,我想按照这个页面的方式来做:

http://www.pikanya.net/testcache/

http://www.pikanya.net/testcache/

Enter something, submit, and click Back button. No warning, it just goes back.

输入内容,提交,然后单击返回按钮。没有警告,它只是返回。

Googling I found out that this might be a bug in Firefox 3, but I'd like to somehow get this behavior even after they "fix" it.

谷歌搜索我发现这可能是 Firefox 3 中的一个错误,但即使在他们“修复”它之后我也想以某种方式获得这种行为。

I guess it could be doable with some HTTP headers, but which exactly?

我想它可以用一些 HTTP 标头来实现,但究竟是哪一个?

采纳答案by Pete Kirkham

One way round it is to redirect the POST to a page which redirects to a GET - see Post/Redirect/Get on wikipedia.

一种方法是将 POST 重定向到重定向到 GET 的页面 - 请参阅维基百科上的 Post/Redirect/Get

Say your POST is 4K of form data. Presumably your server does something with that data rather than just displaying it once and throwing it away, such as saving it in a database. Keep doing that, or if it's a huge search form create a temporary copy of it in a database that gets purged after a few days or on a LRU basis when a space limit is used. Now create a representation of the data which can be accessed using GET. If it's temporary, generate an ID for it and use that as the URL; if it's a permanent set of data it probably has an ID or something that can be used for the URL. At the worst case, an algorithm like tiny url uses can collapse a big URL to a much smaller one. Redirect the POST to GET the representation of the data.

假设您的 POST 是 4K 的表单数据。据推测,您的服务器会对这些数据执行某些操作,而不仅仅是显示一次然后将其丢弃,例如将其保存在数据库中。继续这样做,或者如果它是一个巨大的搜索表单,请在数据库中创建一个临时副本,该副本在几天后或在使用空间限制时基于 LRU 被清除。现在创建可以使用 GET 访问的数据的表示。如果是临时的,则为其生成一个 ID 并将其用作 URL;如果它是一个永久的数据集,它可能有一个 ID 或可用于 URL 的东西。在最坏的情况下,使用像 tiny url 这样的算法可以将大 URL 折叠为小得多的 URL。重定向 POST 以获取数据的表示。



As a historical note, this technique was established practice in 1995.

作为历史记录,这种技术是在 1995 年确立的做法

回答by Ilya Birman

See my golden rule of web programming here:

在这里查看我的网络编程黄金法则:

Stop data inserting into a database twice

停止将数据插入数据库两次

It says: “Never ever respond with a body to a POST-request. Always do the work, and then respond with a Location: header to redirect to the updated page so that browser requests it with GET”

它说:“永远不要用正文响应 POST 请求。总是做这项工作,然后用 Location: 标头响应以重定向到更新的页面,以便浏览器使用 GET 请求它”

If browser ever asks user about re-POST, your web app is broken. User should not ever see this question.

如果浏览器询问用户重新 POST 的问题,则您的 Web 应用程序已损坏。用户不应该看到这个问题。

回答by Sparr

One way to avoid that warning/behavior is to do the POST via AJAX, then send the user to another page (or not) separately.

避免该警告/行为的一种方法是通过 AJAX 进行 POST,然后将用户单独发送到另一个页面(或不发送)。

回答by rgbflawed

I have been using the Session variable to help in this situation. Here's the method I use that has been working great for me for years:

我一直在使用 Session 变量来帮助解决这种情况。这是我使用的多年来一直对我有用的方法:

//If there's something in the POST, move it to the session and then redirect right back to where we are
if ($_POST) {
    $_SESSION['POST']=$_POST;
    redirect($_SERVER["REQUEST_URI"]);
}

//If there's something in the SESSION POST, move it back to the POST and clear the SESSION POST
if ($_SESSION['POST']) {
    $_POST=$_SESSION['POST'];
    unset($_SESSION['POST']);
}

Technically you don't even need to put it back into a variable called $_POST. But it helps me in keeping track of what data has come from where.

从技术上讲,您甚至不需要将其放回名为 $_POST 的变量中。但它帮助我跟踪哪些数据来自何处。

回答by troelskn

I have an application that supplies long list of parameters to a web page, so I have to use POST instead of GET. The problem is that when page gets displayed and user clicks the Back button, Firefox shows up a warning:

我有一个向网页提供一长串参数的应用程序,所以我必须使用 POST 而不是 GET。问题是当页面被显示并且用户点击后退按钮时,Firefox 会显示一个警告:

Your reasoning is wrong. If the request is without side effects, it should be GET. If it has side effects, it should be POST. The choice should not be based on the number of parameters you need to pass.

你的推理是错误的。如果请求没有副作用,应该是GET。如果有副作用,应该是POST。选择不应基于您需要传递的参数数量。

回答by Eugen Konkov

As another solution you may stop to use redirecting at all.

作为另一种解决方案,您可以完全停止使用重定向。

You may process and render the processing result at once with no POSTconfirmation alert. You should just manipulate the browser history object:

您可以立即处理并呈现处理结果,无需POST确认提示。您应该只操作浏览器历史记录对象:

history.replaceState("", "", "/the/result/page")

See fullor shortanswers

查看完整简短的答案