我想阻止用户意外发表评论两次。 我使用PRG(post重定向get)方法,以便将数据插入另一个页面,然后将用户重定向到显示注释的页面。这允许用户根据需要刷新多次。但是,当用户返回并单击再次提交或单击提交100次时,这不起作用。我不想要100条相同的评论。有哪些方法可以防止表单中的重复发布? (PHP)
我看了SO上的相关问题,发现一个令牌是最好的。但是我在使用它时遇到了麻烦。
//makerandomtoken(20) returns a random 20 length char.
<form method="post" ... >
<input type="text" id="comments" name="comments" class="commentbox" /><br/>
<input type="hidden" name="_token" value="<?php echo $token=makerandomtoken(20); ?>" />
<input type="submit" value="submit" name="submit" />
</form>
if (isset($_POST['submit']) && !empty($comments))
{
$comments= mysqli_real_escape_string($dbc,trim($_POST['comments']));
//how do I make the if-statment to check if the token has been already set once?
if (____________){
//don't insert comment because already clicked submit
}
else{
//insert the comment into the database
}
}
所以我有令牌作为隐藏值,但如何使用它来防止多次点击提交。
方法: 有人建议使用会话。我会将随机标记设置为$ _SESSION ['_ token']并检查该会话标记是否等于$ _POST ['_ token'],但我该怎么做?当我尝试时,它仍然不检查
您可能要修改标题,但对问题的其余部分不是100%准确。 – jcolebrand 2010-05-25 03:03:25