use*_*510 118 html php forms post form-submit
我有一个简单的表单,将文本提交到我的SQL表.问题是,在用户提交文本后,他们可以刷新页面并再次提交数据,而无需再次填写表单.我可以在提交文本后将用户重定向到另一个页面,但我希望用户保持在同一页面上.
我记得读过一些关于为每个用户提供一个唯一的会话ID并将其与另一个值进行比较的方法,这个值解决了我遇到的问题,但我忘记了它的位置.
Kev*_*erw 85
使用发布/重定向/获取模式.http://en.wikipedia.org/wiki/Post/Redirect/Get
通过我的网站,我将在cookie或会话中存储消息,在帖子后重定向,读取cookie /会话,然后清除该会话或cookie变量的值.
dtb*_*ker 80
我还想指出,您可以使用javascript方法,window.history.replaceState以防止重新提交刷新和后退按钮.
<script>
if ( window.history.replaceState ) {
window.history.replaceState( null, null, window.location.href );
}
</script>
Run Code Online (Sandbox Code Playgroud)
这里的概念证明:https://dtbaker.net/files/prevent-post-resubmit.php
我仍然会推荐Post/Redirect/Get方法,但这是一个新颖的JS解决方案.
Tan*_*tta 24
这种方法对我很有效,我认为这是完成这项工作的最简单的方法。
一般的想法是在表单提交后将用户重定向到其他一些页面,这将在页面刷新时停止表单重新提交。尽管如此,如果您需要在表单提交后将用户保持在同一页面上,您可以通过多种方式来实现,但这里我描述的是 JavaScript 方法。
这种方法非常简单,一旦提交表单,就会阻止在刷新时要求重新提交表单的弹出窗口。只需将这行 JavaScript 代码放在文件的页脚,就可以看到“魔法”。
<script>
if ( window.history.replaceState ) {
window.history.replaceState( null, null, window.location.href );
}
</script>Run Code Online (Sandbox Code Playgroud)
Moo*_*oob 16
您应该使用Post Redirect Get模式来处理这个问题,但是如果您以某种方式最终处于PRG不可行的位置(例如,表单本身位于include,防止重定向),您可以散列一些请求参数根据内容创建一个字符串,然后检查您是否已经发送过它.
//create digest of the form submission:
$messageIdent = md5($_POST['name'] . $_POST['email'] . $_POST['phone'] . $_POST['comment']);
//and check it against the stored value:
$sessionMessageIdent = isset($_SESSION['messageIdent'])?$_SESSION['messageIdent']:'';
if($messageIdent!=$sessionMessageIdent){//if its different:
//save the session var:
$_SESSION['messageIdent'] = $messageIdent;
//and...
do_your_thang();
} else {
//you've sent this already!
}
Run Code Online (Sandbox Code Playgroud)
Sav*_*voo 13
你可以通过会话变量Like来进行表单重新提交
首先,你必须在表格页面上设置文本框中的rand()和$ _SESSION ['rand']
<form action="" method="post">
<?php
$rand=rand();
$_SESSION['rand']=$rand;
?>
<input type="hidden" value="<?php echo $rand; ?>" name="randcheck" />
Your Form's Other Field
<input type="submit" name="submitbtn" value="submit" />
</form>
Run Code Online (Sandbox Code Playgroud)
用文本框$ _POST ['randcheck']检查$ _SESSION ['rand']之后的值
if(isset($_POST['submitbtn']) && $_POST['randcheck']==$_SESSION['rand'])
{
// Your code here
}
Run Code Online (Sandbox Code Playgroud)
Ibu*_*Ibu 12
处理表单后,您将重定向到另一个页面:
... process complete....
header('Location: thankyou.php');
Run Code Online (Sandbox Code Playgroud)
您也可以重定向到同一页面.
如果您正在执行类似注释的操作并且希望用户保持在同一页面上,则可以使用Ajax来处理表单提交
Mo'*_*med 12
我使用这个javascript行来阻止弹出窗口一旦提交表单就要求刷新表单重新提交.
if ( window.history.replaceState ) {
window.history.replaceState( null, null, window.location.href );
}
Run Code Online (Sandbox Code Playgroud)
只需将此行放在文件的页脚上即可看到魔法
Pra*_*dra 11
使用标题并重定向页面.
header("Location:your_page.php"); 您可以重定向到同一页面或不同页面.
将数据插入数据库后取消设置$ _POST.
unset($_POST);
我找到了下一个解决方法.您可以POST通过操作history对象来处理请求后转义重定向.
所以你有HTML表单:
<form method=POST action='/process.php'>
<input type=submit value=OK>
</form>
Run Code Online (Sandbox Code Playgroud)
当您在服务器上处理此表单时,您可以/the/result/page通过设置Location标题来重定向用户,如下所示:
$cat process.php
<?php
process POST data here
...
header('Location: /the/result/page');
exit();
?>
Run Code Online (Sandbox Code Playgroud)
在处理POSTed数据后,您渲染小<script>和结果/the/result/page
<?php
process POST data here
render the <script> // see below
render `/the/result/page` // OK
?>
Run Code Online (Sandbox Code Playgroud)
在<script>您应该呈现:
<script>
window.onload = function() {
history.replaceState("", "", "/the/result/page");
}
</script>
Run Code Online (Sandbox Code Playgroud)
结果是:
正如您所见,表单数据被POST编辑为process.php脚本.
这个脚本程序POST编数据,并绘制/the/result/page一次有:
POST刷新页面时没有重新数据(F5)POST通过浏览器历史记录导航到上一页/下一页时无法重复UPD
作为另一种解决方案我问功能要求的Mozilla Firefox的团队,以允许用户设置NextPage标头,会像Location头,使post/redirect/get模式已经过时.
简而言之.当服务器处理表单POST数据成功时:
NextPage标头而不是LocationPOST表单数据的结果,因为它将GET在post/redirect/get模式中呈现请求浏览器依次看到NextPage标题:
window.location随NextPage价值调整GET请求NextPage而不是重新POST形成数据我认为如果实施这将是非常好的,不是吗? =)
小智 7
一个非常可靠的方法是在帖子中实现一个唯一的ID并将其缓存在
<input type='hidden' name='post_id' value='".createPassword(64)."'>
Run Code Online (Sandbox Code Playgroud)
然后在你的代码中这样做:
if( ($_SESSION['post_id'] != $_POST['post_id']) )
{
$_SESSION['post_id'] = $_POST['post_id'];
//do post stuff
} else {
//normal display
}
function createPassword($length)
{
$chars = "abcdefghijkmnopqrstuvwxyz023456789";
srand((double)microtime()*1000000);
$i = 0;
$pass = '' ;
while ($i <= ($length - 1)) {
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
Run Code Online (Sandbox Code Playgroud)
Javascript
这种方法非常简单,一旦提交表单,就会阻止在刷新时要求重新提交表单的弹出窗口。只需将这行 javascript 代码放在文件的页脚,就可以看到神奇之处。
<script>
if ( window.history.replaceState ) {
window.history.replaceState( null, null, window.location.href );
}
</script>
Run Code Online (Sandbox Code Playgroud)