php PHP清除浏览器缓存以避免刷新后重新发送表单数据

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

PHP clear browser cache to avoid form data resend after refresh

phpcachingbrowser

提问by Jorge

I'm developing a PHP-MySQL app that enables registered users to enter text comments. Problem is:

我正在开发一个 PHP-MySQL 应用程序,它使注册用户能够输入文本注释。问题是:

  1. User sign-in into the web site - OKAY
  2. User presented with a form to submit text comment - OKAY
  3. User enters text comment and submits - OKAY
  4. I have a routine that sanitize the comment & save it into MySQL (with userid, textcomment, datetime stamp) & present back the user that his/her comment is entered - OKAY
  5. User decides to refresh browser - a duplicate comment is entered - BAD!
  1. 用户登录网站 - OKAY
  2. 用户提供了一个表单来提交文本评论 - OKAY
  3. 用户输入文本评论并提交 - OKAY
  4. 我有一个例程来清理评论并将其保存到 MySQL 中(使用用户 ID、文本评论、日期时间戳)并向用户展示他/她的评论已输入 - OKAY
  5. 用户决定刷新浏览器 - 输入了重复的评论 - 糟糕!

I was thinking 3 options:

我在想 3 个选项:

  • OPTION 1: Routine that checks: last time user posted comment, and if so, check if this is a duplicate. If duplicate then display error message.
  • OPTION 2: Routine that does not allow a user to post too quickly. So basically do not allow postings of comments within 1 minute or so. So if browser is refreshed the comment will be ignored.
  • OPTION 3: Manipulate the browser cache to clear out its contents so when refreshed no duplicate will be entered.
  • 选项 1:检查的例程:用​​户上次发表评论的时间,如果是,请检查这是否是重复的。如果重复,则显示错误消息。
  • 选项 2:不允许用户过快发布的例程。所以基本不允许1分钟左右的评论发帖。因此,如果刷新浏览器,评论将被忽略。
  • 选项 3:操作浏览器缓存以清除其内容,以便刷新时不会输入重复项。

Now in the context of my application, my concerns with OPTION 1 and OPTION 2 is performance PHP-MySQL since I already have various queries within the same page that push/get data from databases. So OPTION 3 may target the issue differently.

现在在我的应用程序的上下文中,我对 OPTION 1 和 OPTION 2 的担忧是性能 PHP-MySQL,因为我已经在同一页面中进行了从数据库推送/获取数据的各种查询。因此,选项 3 可能针对不同的问题。

Questions is: If I go for OPTION 3 can this be considered a Best Practice? meaning clearing the browser cache is the best most effective solution? I have read that there are consequences too? your thoughts are appreciated!

问题是:如果我选择选项 3,这可以被视为最佳实践吗?意思是清除浏览器缓存是最好最有效的解决方案?我读过也有后果?您的想法值得赞赏!

采纳答案by Klaus Byskov Pedersen

Just do a redirect after submitting data to the database. It's a common practise.

只需在将数据提交到数据库后进行重定向即可。这是一种常见的做法。

An http redirectinstructs the browser to issue an http GETfor the url specified (as opposed to the http POSTthat is used to submit the form) . If you do this right after you have inserted data into the database, when the user refreshes his browser nothing will happen other than him seeing the same page again.

一个HTTP重定向指示浏览器发出HTTPGET指定的URL(而不是在HTTPPOST用来提交表单)。如果您在将数据插入数据库后立即执行此操作,则当用户刷新浏览器时,除了再次看到同一页面之外,不会发生任何事情。

This question on SOtells how you redirect with php.

SO 上的这个问题告诉您如何使用 php 重定向。

回答by LoneWOLFs

Just unset the posted variable after you have inserted it in the database

将发布的变量插入数据库后,只需取消设置它

<?php
    if(isset($_POST["comment"])){
        //store in the database
        //on successful storage
        unset($_POST["comment"]);
    }
?>

Thus the value won't be posted back when user refreshes the page...

因此,当用户刷新页面时,该值不会被回传......

回答by AndreKR

You need to implement the Post/Redirect/Get pattern.

您需要实现Post/Redirect/Get 模式

回答by Gumbo

I would use Option 4: Use a one-time form token that authenticates the form request. If the request was successful, invalidate the token.

我将使用选项 4:使用对表单请求进行身份验证的一次性表单令牌。如果请求成功,则使令牌无效。

When doing this, even returning to the form after a redirection won't allow it to send the form again. Additionally, this will also make CSRF attacks harder.

这样做时,即使在重定向后返回表单也不允许它再次发送表单。此外,这也会使 CSRF 攻击更加困难。

回答by Pradeep Singh

After entering data into database redirect the page using header or location.href

将数据输入数据库后,使用 header 或 location.href 重定向页面

回答by nivas

i have found new way try this.

我找到了新方法试试这个。

   function PreventResendData($invalidpage='index.php'){

    if(   $_POST && !is_array( $_SESSION['post_var'] )   ) {

        $_SESSION['post_var'] = $_POST;
        header('location:'.$_SERVER['PHP_SELF']);

    }
    else if($_SESSION['post_var'])
    {
        $_POST = $_SESSION['post_var']; 
        $_SESSION['post_var'] = '';
    }
    else
    {
        header("location:".$invalidpage);
    }

}