php 如何避免在php中刷新时重新发送数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2666882/
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
How to avoid resending data on refresh in php
提问by Priyanka
I have a page "index.php" where i have a link called "add_users.php". In "add_users.php", I accept user information and come back to the same page "index.php" where information comes through post action and gets inserted into the database.
我有一个页面“index.php”,其中有一个名为“add_users.php”的链接。在“add_users.php”中,我接受用户信息并返回到同一页面“index.php”,其中信息通过发布操作获得并插入到数据库中。
When I refresh the page or hit the back button, a resend box appears. I went through many solution where they asked me to create third page. I tried doing that as follows: after inserting values in database, I redirected the page as header('Location:http://thisisawebsite.com/thankyou.php, and in thankyou.php I again redirected the page to index.php. But this resulted in getting a warning as "Cannot modify header information - headers already sent by [....]"
当我刷新页面或点击后退按钮时,会出现一个重新发送框。我经历了许多解决方案,他们要求我创建第三页。我尝试这样做:在数据库中插入值后,我将页面重定向为 header('Location: http://thisisawebsite.com/thankyou.php,在thankyou.php 中,我再次将页面重定向到 index.php。但这导致收到警告“无法修改标头信息 - [....] 已发送标头”
What is a better solution?
什么是更好的解决方案?
采纳答案by Karthik
Please use ob_start(); statement in first line itself.
请使用 ob_start(); 第一行本身的语句。
回答by Decent Dabbler
Priyanka,
普里扬卡,
You are on the right track. What you are trying to implement is actually a well known pattern used in web-developing called the POST/Redirect/GET pattern. (Pattern is a bit of a buzz word now-a-days, so maybe paradigm is a better word for this).
你走在正确的轨道上。您尝试实现的实际上是一种在 Web 开发中使用的众所周知的模式,称为POST/Redirect/GET 模式。(模式现在有点流行,所以也许范式是一个更好的词)。
A common implementation of this pattern/paradigm is to simply have only one point of entry.
这种模式/范式的常见实现是只有一个入口点。
By doing this, add_user.phpcould now look like this (it's still not the most elegant, but hopefully it will give you an idea of how to go about implementing it):
通过这样做,add_user.php现在可以看起来像这样(它仍然不是最优雅的,但希望它能让您了解如何实现它):
<?php
// is this a post request?
if( !empty( $_POST ) )
{
/*
process the form submission
and on success (a boolean value which you would put in $success), do a redirect
*/
if( $success )
{
header( 'HTTP/1.1 303 See Other' );
header( 'Location: http://www.example.com/add_user.php?message=success' );
exit();
}
/*
if not successful, simply fall through here
*/
}
// has the form submission succeeded? then only show the thank you message
if( isset( $_GET[ 'message' ] ) && $_GET[ 'message' ] == 'success' )
{
?>
<h2>Thank you</h2>
<p>
You details have been submitted succesfully.
</p>
<?php
}
// else show the form, either a clean one or with possible error messages
else
{
?>
<!-- here you would put the html of the form, either a clean one or with possible error messages -->
<?php
}
?>
So, how it basically works is this:
所以,它的基本工作原理是这样的:
- If the request made to the script IS NOT a POST request (i.e. the form has not been submitted) and there is no
?message=successappended to the url then simply show a clean form. - If the request made to the script IS a POST request, then process the form.
- If form processing succeeded, redirect to the same script again, and append
?message=success- If the request to the script is a request with
?message=successappended to it only show a thank you message, don't show the form.
- If the request to the script is a request with
- If form processing failed, then let it 'fall through' and show the form again, but this time with some descriptive error messages and with the form elements filled with what the user already had filled in.
- If form processing succeeded, redirect to the same script again, and append
- 如果对脚本的请求不是 POST 请求(即表单尚未提交)并且没有
?message=success附加到 url,则只需显示一个干净的表单。 - 如果对脚本发出的请求是 POST 请求,则处理该表单。
- 如果表单处理成功,再次重定向到同一个脚本,并追加
?message=success- 如果对脚本的请求是
?message=success附加到它的请求只显示感谢信息,则不要显示表单。
- 如果对脚本的请求是
- 如果表单处理失败,则让它“通过”并再次显示表单,但这次会出现一些描述性错误消息,并且表单元素填充了用户已经填写的内容。
- 如果表单处理成功,再次重定向到同一个脚本,并追加
Hopefully this, along with the example I gave you, makes enough sense.
希望这,连同我给你的例子,有足够的意义。
Now, the reason you were getting the infamous Warning: headers already sentmessage is explained in this answer I gaveto another question about why certain php calls are better to put at the top of a script (actually, it doesn't necessarily have to be on top, but it has to be called before ANY output (even (accidental) whitespace) is being output).
现在,你收到这条臭名昭著的Warning: headers already sent消息的原因在我给另一个问题的答案中解释了为什么某些 php 调用最好放在脚本的顶部(实际上,它不一定必须在顶部,但是它必须在输出任何输出(甚至(偶然的)空格)之前被调用)。
回答by ta.speot.is
But getting warning as Cannot modify header information - headers already sent
但收到警告无法修改标头信息 - 标头已发送
Is there something wrong with using output buffering? Why does your page send headers even though it's only doing work in the database? Do you have a space or a newline before the first "
使用输出缓冲有什么问题吗?为什么您的页面即使只在数据库中工作,也会发送标题?在第一个“之前是否有空格或换行符”
You've got the solution, you should just make it work.
你已经有了解决方案,你应该让它发挥作用。
回答by TechniXP
I think that using sessions is best way to prevent this problem
我认为使用会话是防止此问题的最佳方法
session_start();
if(!isset($_SESSION['s'])){
$_SESSION['s'] = true;
};
if( !empty( $_POST ) && ($_SESSION['s']) )
{
//your code
$_SESSION['s'] = false;
}
Afterwards you may use unset($_SESSION['s'])or destroy_session()if you want users to be able to post something again.
之后您可以使用unset($_SESSION['s'])或者destroy_session()如果您希望用户能够再次发布内容。
回答by AugustoM
Same challenge I faced using a comment box, what I did is to redirect the page to the same page and to an specific "anchor". Example:
我在使用评论框时遇到了同样的挑战,我所做的是将页面重定向到同一页面和特定的“锚点”。例子:
<form id="myForm" action="<?php echo $_SERVER['PHP_SELF'];?>#myForm" method="post">
<!--Stuff for form in here -->
</form>
Then for the PHP code I have:
然后对于PHP代码我有:
<?php
//Do your stuff and when finished...
$reloadpage = $_SERVER['PHP_SELF']."#myForm";
header("Location:$reloadpage");
exit();
?>
This avoid the F5 resend of the data from the Form
这避免了 F5 重新发送表单中的数据
回答by Gobi
Use meta refresh
使用元刷新
<%meta http-equiv="refresh" content="5;url=http://example.com/" />" at the time of your success message after adding data into the DB.
<%meta http-equiv="refresh" content="5;url= http://example.com/" />" 在您将数据添加到数据库后的成功消息时。
may be its trick to avoid reloading .
可能是它避免重新加载的技巧。
Thanxs, Gobi.
谢谢,戈壁。

