如何在使用 PHP 的浏览器上按下“刷新”按钮时删除 $_POST 变量?

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

How to delete $_POST variable upon pressing 'Refresh' button on browser with PHP?

php

提问by Navneet

When I press the 'refresh' button on my browser, it seems that $_POSTvariable is preserved across the refresh.

当我按下浏览器上的“刷新”按钮时,似乎$_POST在刷新过程中保留了该变量。

If I want to delete the contents of $_POSTwhat should I do? Using unsetfor the fields of $_POSTdid not help.

如果我想删除内容$_POST应该怎么做?使用unset的领域$_POST并没有帮助。

Help? Thanks!

帮助?谢谢!

采纳答案by burntblark

The request header contains some POST data. No matter what you do, when you reload the page, the rquest would be sent again.

请求头包含一些 POST 数据。无论您做什么,当您重新加载页面时,都会再次发送请求。

The simple solution is to redirect to a new (if not the same) page. This pattern is very common in web applications, and is called Post/Redirect/Get. It's typical for all forms to do a POST, then if successful, you should do a redirect.

简单的解决方案是重定向到一个新的(如果不是相同的)页面。这种模式在 Web 应用程序中非常常见,称为Post/Redirect/Get。所有表单都执行 POST 是典型的,然后如果成功,您应该执行重定向。

Try as much as possible to always separate (in different files) your view script (html mostly) from your controller script (business logic and stuff). In this way, you would always post data to a seperate controller script and then redirect back to a view script which when rendered, will contain no POST data in the request header.

尽可能地将您的视图脚本(主要是 html)与您的控制器脚本(业务逻辑和东西)分开(在不同的文件中)。通过这种方式,您将始终将数据发布到单独的控制器脚本,然后重定向回视图脚本,该脚本在呈现时将在请求标头中不包含 POST 数据。

回答by Peter

To prevent users from refreshing the page or pressing the back button and resubmitting the form I use the following neat little trick.

为了防止用户刷新页面或按下后退按钮并重新提交表单,我使用了以下巧妙的小技巧。

<?php

if (!isset($_SESSION)) {
    session_start();
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $_SESSION['postdata'] = $_POST;
    unset($_POST);
    header("Location: ".$_SERVER['PHP_SELF']);
    exit;
}
?>

The POST data is now in a session and users can refresh however much they want. It will no longer have effect on your code.

POST 数据现在处于会话中,用户可以随意刷新。它不会再对您的代码产生影响。

回答by raversparadise.co.uk

Simple PHP solution to this:

简单的 PHP 解决方案:

if (isset($_POST['aaa'])){
echo '
<script type="text/javascript">
location.reload();
</script>';
}

As the page is reloaded it will update on screen the new data and clear the $_POST ;)

当页面重新加载时,它将在屏幕上更新新数据并清除 $_POST ;)

回答by Kai Qing

this is a common question here.

这是这里的一个常见问题。

Here's a link to a similar question. You can see my answer there. Why POST['submit'] is set when I reload?

这是一个类似问题的链接。你可以在那里看到我的回答。 为什么重新加载时设置了 POST['submit']?

The basic answer is to look into post/redirect/get, but since it is easier to see by example, just check the link above.

基本答案是查看 post/redirect/get,但由于通过示例更容易查看,只需查看上面的链接即可。

回答by alx

My usual technique for this is:

我通常的技术是:

<?php
if ($_POST) {
   $errors = validate_post($_POST);

   if ($!errors) {
       take_action($_POST);
       // This is it (you may want to pass some additional parameters to generate visual feedback later):
       header('Location: ?');
       exit;
   }
}
?>

回答by Otvazhnii

How about using $_POST = array(), which nullifies the data. The browser will still ask to reload, but there will be no data in the $_POST superglobal.

如何使用 $_POST = array(),它会使数据无效。浏览器仍会要求重新加载,但 $_POST 超全局变量中将没有数据。

回答by Eliseu Monar dos Santos

You can't, this is treated by the browser, not by any programming language. You can use AJAX to make the request or redirect the user to the same (or another) page.

你不能,这是由浏览器处理的,而不是任何编程语言。您可以使用 AJAX 发出请求或将用户重定向到同一个(或另一个)页面。

回答by Daff

$_POST should only get populated on POST requests. The browser usually sends GET requests. If you reached a page via POST it usually asks you if it should resend the POST data when you hit refresh. What it does is simply that - sending the POST data again. To PHP that looks like a different request although it semantically contains the same data.

$_POST 应该只在 POST 请求中填充。浏览器通常发送 GET 请求。如果您通过 POST 到达某个页面,它通常会在您点击刷新时询问您是否应重新发送 POST 数据。它所做的只是 - 再次发送 POST 数据。对于看起来像不同请求的 PHP,尽管它在语义上包含相同的数据。

回答by exussum

The "best" way to do this is Post / Redirect / Get

做到这一点的“最佳”方法是发布/重定向/获取

http://en.wikipedia.org/wiki/Post/Redirect/Get

http://en.wikipedia.org/wiki/Post/Redirect/Get

After the post send a 302 header pointing to the success page

发帖后发送一个指向成功页面的302标头

回答by Matteo

try

尝试

unset($_POST);
unset($_REQUEST);
header('Location: ...');

it worked for me

它对我有用