Prevent Double Form Submit using Tokens

前端 未结 7 1786
忘掉有多难
忘掉有多难 2020-12-10 07:44

I am trying to prevent the user from double submitting the forum by adding token hidden field.

So here is what I have done so far (before the forum loads I have this

7条回答
  •  孤城傲影
    2020-12-10 08:39

    I had the same problem, here is a simple fix:

    if(!empty($_SESSION['form_token']) && time() - $_SESSION['form_token'] < 3){
        $data['message'] = 'try again later';
        return;
    }
    $_SESSION['form_token'] = time();
    

    In my case the PRG pattern didn't have any effect since form submitted multiple times at the same time and the code had not been executed and there is no data saved to compare it against.

提交回复
热议问题