简体   繁体   English

PHP-缓存控制在我的情况下不起作用

[英]PHP - Cache Control is not working in my scenario

i need some help, i got a php file which is called shoppingcart.php and the data of chosen product will post to this page for sql insert, and if i reload this page by f5 or press "Backspace" at next page, the same data will re-insert again so i don't want this to happen and been doing some searching but still not solving my problem. 我需要一些帮助,我得到了一个名为shoppingcart.php的php文件,所选产品的数据将发布到此页面以进行sql插入,如果我通过f5重新加载此页面或在下一页按“退格”,则同样数据将再次插入,所以我不希望这种情况发生,并且一直在进行一些搜索,但仍然不能解决我的问题。

My Code: 我的代码:

<?php
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>

<?php
if(isset($_POST["form"])){
//insert data to db
}

ob_start();
?>

<form id="form" name="form" method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>"
enctype="multipart/form-data" onsubmit="return validate();">
....
....
....
</form>

<?php
$pagecontent = ob_get_contents();
ob_end_clean();
$pagetitle = "Shopping Cart";
include("include/template.php");
?>

I have researched on output buffer and http header article but i still can't figure it out why it is still happening. 我已经研究了输出缓冲区和http标头文章,但我仍然无法弄清为什么它仍在发生。 Please help me~ Thanks in advance. 请帮助我〜预先感谢。

Try putting all your Cache-control headers into the same call to header() . 尝试将所有Cache-control标头放入对header()的同一调用中。 The browser may not be expecting multiple Cache-control headers. 浏览器可能不希望有多个缓存控制标头。

Also, are you getting a prompt in the browser that reloading will resubmit POST data? 另外,您是否在浏览器中收到提示,提示重新加载将重新提交POST数据? If so, and you are electing to resubmit, you will need your own code to ensure that force-rePOSTed data is rejected (despite the user's request), eg. 如果是这样,并且您选择重新提交,则需要使用自己的代码来确保强制重新发布的数据被拒绝(尽管有用户的请求),例如。 by including a unique identifier in the form that is checked before re-inserting. 通过在重新插入之前检查的形式包括唯一标识符。 Alternatively you can accept that if the user elects to resubmit POST data, then they are explicitly requesting that the request be re-processed. 或者,您可以接受如果用户选择重新提交POST数据,那么他们明确要求重新处理该请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM