我想阻止用户意外发布两次评论.我使用PRG(post redirect get)方法,以便我将数据插入另一个页面,然后将用户重定向回显示注释的页面.这允许用户根据需要刷新多次.但是,当用户返回并再次单击提交或者他们点击提交100次非常快时,这不起作用.我不想要100个相同的评论.
我在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
}
}
Run Code Online (Sandbox Code Playgroud)
所以我将令牌作为隐藏值,但我如何使用它来防止多次单击提交.
方法: 有人建议使用会话.我会将随机令牌设置为$ _SESSION ['_ token']并检查该会话令牌是否等于$ _POST ['_ token'],但我该怎么做?当我尝试时,它仍然没有检查
如果要阻止双重提交,则必须存储"已提交"与"未提交"的状态.您可以通过多种方式保留此信息.
| 归档时间: |
|
| 查看次数: |
3463 次 |
| 最近记录: |