可能重复:避免在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.
为什么我这样做?我想刷新页面 . 我不希望从浏览器中弹出该消息 . 只需根据以前提交的值刷新页面,因为它在我的数据库中创建重复值 .
3 回答
您需要使用HTTP重定向进行响应,建议的流程是处理POSTed数据,然后重定向到另一个页面,其中包含已处理数据的内部标识符 .
如果刷新刚刚从POST返回的网页,则预期的行为是使另一个POST具有与上次相同的值 .
header
函数可以在php http://php.net/manual/en/function.header.php中用于此目的要使状态保持在重定向中,您可以使用php
$_SESSION
或传递重定向URL的查询字符串中的数据 .阅读发布/重定向/获取设计模式:http://bakery.cakephp.org/articles/luciansabo/2011/08/12/post_redirect_get_design_pattern_component
您收到此消息,因为刷新页面会发送相同的请求,就像之前加载页面时一样 . 在您的情况下,它是表单的提交 . 浏览器警告您,如果刷新页面,POST数据将再次发送,如果脚本不受保护,它可能会在数据库或类似的东西中创建重复值 .
解决此问题的一种方法是将用户重定向到同一页面(重定向时POST数据总是丢失) . 如果您想要安全,即使用户在重定向后按"back"并重新提交数据,here也是解决方案 .