[英]Clear form post data
I have a 3 page system, form > post > submitted, in place. 我有一个3页的系统,表格>帖子>已提交。 The problem I'm having is that if a user presses the back button from the submitted page, it takes them to the form page, with the values filled in, so if they click submit, it re-inputs the data.
我遇到的问题是,如果用户按下提交页面中的“后退”按钮,它将带它们进入表单页面,并填充了值,因此如果他们单击“提交”,它将重新输入数据。
In the usual case, the user goes back to the index page normally, and there's a process by which we check how many times the users ip has appeared in said database (mysql) in the past hour, and limit post ability based on that. 在通常情况下,用户会正常返回索引页面,并且存在一个过程,通过该过程我们可以检查用户ip在过去一个小时内出现在所述数据库(mysql)中的次数,并以此为基础来限制发布能力。 But if they press back, that system is void.
但是,如果他们压回去,该系统将失效。
The only code I have in place to prevent this is: 我唯一可以防止这种情况的代码是:
ob_start();
header('Cache-Control: private');
But that seems to do nothing. 但这似乎无济于事。
Any help would be much appreciated! 任何帮助将非常感激!
I have a quick solution for it: 我有一个快速的解决方案:
<?php
//Check for double post
//Start session
session_start();
$process = true;
if(isset($_POST)){
if(isset($_SESSION['olddata'])){
if($_SESSION['olddata'] == $_POST){
$process = false;
}
$_SESSION['olddata'] == $_POST;
}
}
//Now you can check $process before you do something with the data
?> ?>
Put the POST check right before the data is submitted or redirect (change header location) to another success/submitted page that contains the information required. 将POST检查放在数据提交之前,或将其重定向(更改标头位置)到包含所需信息的另一个成功/提交页面。 That way if they click back they go back to the original submitted page that redirects them back to the success/submitted page.
这样,如果他们单击返回,将返回到原始提交页面,该页面将其重定向回成功/提交页面。
There are other solutions but it all depends on the purpose- is this to prevent a standard user making a silly mistake or a malicious user. 还有其他解决方案,但都取决于目的-这是为了防止标准用户犯傻错误或恶意用户。
You could try: 您可以尝试:
header('Cache-Control: no-cache');
I can't see how enabling output buffering would prevent this behaviour, so you may wish to omit the ob_start() and ob_flush() calls. 我看不到启用输出缓冲将如何防止此行为,因此您可能希望省略ob_start()和ob_flush()调用。
But I do agree it would be better to do: 但我同意这样做会更好:
header("Location: mypage.php?somepublicdata=this+is+public");
and then use $_SESSION to carry over the $_POST data. 然后使用$ _SESSION继承$ _POST数据。
Shouldn't something like this work? 这样的事情不应该吗? (I haven't tried it out myself.)
(我自己还没有尝试过。)
<head>
<script>
function clearForms()
{
var i;
for (i = 0; (i < document.forms.length); i++) {
document.forms[i].reset();
}
}
</script>
</head>
<body onLoad="clearForms()" onUnload="clearForms()">
</body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.