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

后端 未结 23 1307
醉话见心
醉话见心 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:44

    What Works For Me is :

    if ( !refreshed()) {
       //Your Submit Here
            if (isset( $_GET['refresh'])) {
                setcookie("refresh",$_GET['refresh'], time() + (86400 * 5), "/");
            }
    
        }    
    }
    
    
    function refreshed()
    {
        if (isset($_GET['refresh'])) {
            $token = $_GET['refresh'];
            if (isset($_COOKIE['refresh'])) {
                if ($_COOKIE['refresh'] != $token) {
                    return false;
                } else {
                    return true;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    }  
    
    
    function createToken($length) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
        return $randomString;
    }
    
    ?>
    

    And in your Form

     <form  action="?refresh=<?php echo createToken(3)?>">
    
    
    
     </form>
    
    0 讨论(0)
  • 2020-11-22 01:49

    You can prevent form resubmission via a session variable.

    First you have to set rand() in a textbox and $_SESSION['rand'] on the form page:

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

    After that check $_SESSION['rand'] with textbox $_POST['randcheck'] value like this:

    if(isset($_POST['submitbtn']) && $_POST['randcheck']==$_SESSION['rand'])
    {
        // Your code here
    }
    
    0 讨论(0)
  • 2020-11-22 01:50

    As others have said, it is not possible to out of using post/redirect/get. But at the same time it is quite easy to do what you want to do server side.

    In your POST page you simply validate the user input but do not act on it, instead you copy it into a SESSION array. You then redirect back to the main submission page again. Your main submission page starts by checking to see if the SESSION array that you are using exists, and if so copy it into a local array and unset it. From there you can act on it.

    This way you only do all your main work once, achieving what you want to do.

    0 讨论(0)
  • 2020-11-22 01:52
    if (($_SERVER['REQUEST_METHOD'] == 'POST') and (isset($_SESSION['uniq']))){
        if($everything_fine){
            unset($_SESSION['uniq']);
        }
    }
    else{
        $_SESSION['uniq'] = uniqid();
    }
    
    0 讨论(0)
  • 2020-11-22 01:53

    This method works for me well, and I think this is the simplest one to do this job.

    The general idea is to redirect the user to some other pages after the form submission, which would stop the form resubmission on page refresh. Still, if you need to hold the user on the same page after the form is submitted, you can do it in multiple ways, but here I am describing the javascript method.

    Javascript Method

    This method is quite easy and blocks the pop up asking for form resubmission on refresh once the form is submitted. Just place this line of javascript code at the footer of your file and see the magic.

    <script>
    if ( window.history.replaceState ) {
      window.history.replaceState( null, null, window.location.href );
    }
    </script>

    0 讨论(0)
提交回复
热议问题