Unset post variables after form submission

前端 未结 10 1135
礼貌的吻别
礼貌的吻别 2020-12-10 14:19

Is there a way to do the above? Basically, I don\'t want the form to be submitted again if someone presses refresh after already submitting the form once. In which case the

相关标签:
10条回答
  • 2020-12-10 15:11

    You need to use the POST/REDIRECT/GET pattern for this.

    Post/Redirect/Get (PRG) is a web development design pattern that prevents some duplicate form submissions, creating a more intuitive interface for user agents (users). PRG supports bookmarks and the refresh button in a predictable way that does not create duplicate form submissions.

    When a web form is submitted to a server through an HTTP POST request, a web user that attempts to refresh the server response in certain user agents can cause the contents of the original POST request to be resubmitted, possibly causing undesired results, such as a duplicate web purchase.

    To avoid this problem, many web developers use the PRG pattern—instead of returning a web page directly, the POST operation returns a redirection command. The HTTP 1.1 specification introduced the HTTP 303 ("See other") response code to ensure that in this situation, the web user's browser can safely refresh the server response without causing the initial POST request to be resubmitted. However most common commercial applications in use today (new and old alike) still continue to issue HTTP 302 ("Found") responses in these situations.

    Here's an example in PHP:

    header('Location: /yourpage.php', true, 303);
    exit;
    
    0 讨论(0)
  • 2020-12-10 15:13

    Use this code

        if(isset($_POST)){
          header('location:'.$_SERVER['PHP_SELF']);
          die();
         }
    
    0 讨论(0)
  • 2020-12-10 15:14

    The post/redirect/get is a good option as some posters have already mentioned.

    One another way I can think of is to set a session in the dostuff.php page to indicate that the posting has already been done. Check this session var each time to see if the page is being loaded again because of a page refresh.

    <?php
        session_start();
        if(isset($_SESSION['indicator'])) 
        {
            /*
            dont do anything because session indicator says 
            that the processing was already done..
    
            you might want to redirect to a new url here..          
            */
        }   
        else
        {
    
            /*
            first set session indicator so that subsequent 
            process requests will be ignored
            */
            $_SESSION['indicator'] = "processed"; 
    
            //process the request here..
        }
        ?>
    

    In the page you redirect to, unset the session var so that the form can be resubmitted again afresh, making it a new post operation. This will allow new form posts but will prevent post operations due to page refresh

    0 讨论(0)
  • 2020-12-10 15:18

    Here's a nice method I use to keep users from submiting the same data twice, which will also prevent the page from adding the same record to the database when reload.

    // First IF
    if ($_SESSION['dup_comment_body'] == $_POST['comment_body']) { 
        echo 'You already entered that.';
    } else {
        // Second IF
        if ($_POST['comment_body']) {
    
            // Run your query here
    
        $_SESSION['dup_comment_body'] = $_POST['comment_body']; 
        header('location:'.$_SERVER['REQUEST_URI'].'');
        }
    } 
    

    The first IF checks to see if the $_POST is equal to the last thing they typed ($_SESSION). If it's not the same it runs the next IF to test if the $_POST variable is not empty. Inside the last IF towards the bottom it sets $_SESSION['dup_comment_body'] to equal the $_POST. So next time the first IF runs and the $_POST is the same, they will get the message "You already entered that.". Hope this helps!

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