当php页面刷新时,浏览器会弹出一条消息给用户


When the php page is refreshed, browser pops out a message to user

可能重复:
避免在php页面上重新发送表单

Index.html

 <form method="post" action="demo.php">
             <input type="text" name="fname"/> 
             <input type="text" name="lname"/>
 </form>

demo.php

 <?php
       $firstname = $_POST['fname'];
       $lastname = $_POST['lname'];
       //some more php code to fill the webpage
  ?>

因此,用户输入他的名字和姓氏并提交表单,然后demo.php完成它的工作并正常工作,但当我按下F5或刷新demo.php时,下次我从浏览器中弹出

     // this message is from google chrome
     the page that you're looking used information that you entered.
     Returning to that page might cause any action to be repeated.
     Do you want to continute?
    // this message is from IE 7 or 8
    To display the webpage again,Internet Explorer needs to resend information
    you've previously submitted. If you were making a purchase, 
    you should click cancel to avoid duplicate transaction,else click retry.

为什么我会得到这个?我只想刷新页面。我不希望那个消息从浏览器中弹出。只需根据以前提交的值刷新页面,因为它会在我的数据库中创建重复的值。

您需要使用HTTP重定向进行响应,建议的流程是处理POSTed数据,然后重定向到具有所处理数据的内部标识符的另一个页面。

如果刷新刚从POST返回的网页,那么预期的行为是使用与上次相同的值进行另一次POST。

header函数可以在php中用于此目的http://php.net/manual/en/function.header.php

为了在重定向中保持状态,您可以使用php$_SESSION或在重定向URL的查询字符串中传递数据。

阅读Post/Rerect/Get设计模式:http://bakery.cakephp.org/articles/luciansabo/2011/08/12/post_redirect_get_design_pattern_component

您会收到此消息,因为刷新页面会发送与上次加载页面时相同的请求。在您的情况下,这是表单的提交。浏览器警告您,如果您刷新页面,POST数据将再次发送,如果脚本不受保护,它可能会在数据库或类似内容中创建重复值。

解决这个问题的一种方法是将用户重定向到同一页面(POST数据在重定向时总是丢失)。如果您希望安全,即使用户在重定向并重新提交数据后按下"返回"键,以下是解决方案。