How to prevent form resubmission when page is refreshed (F5 / CTRL+R)

后端 未结 23 1325
醉话见心
醉话见心 2020-11-22 00:46

I have a simple form that submits text to my SQL table. The problem is that after the user submits the text, they can refresh the page and the data gets submitted again with

23条回答
  •  太阳男子
    2020-11-22 01:26

    A pretty surefire way is to implement a unique ID into the post and cache it in the

    
    

    Then in your code do this:

    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;
    }
    

提交回复
热议问题