double submission when refresh php

自闭症网瘾萝莉.ら 提交于 2019-12-05 02:51:53

问题


I'm trying to correct the issue of double submit in my script. When I press submit it only updates mysql once(which is what I want). However, when I hit refresh it updates mysql again. It seems to ignore the if statement once the refresh button is hit. What would I do to stop this

here is my code

if (isset($_POST['submitButton'])) { 
//do something
 }


<form action = "table.php" method="post">
<label for="submitButton"></label>
<input type="submit" name="submitButton" id="submitButton"
value="Submit Form"/>
</form>

回答1:


When you refresh the page - the POST IS SENT AGAIN

Some browsers actually warn you about that happening.

To prevent that I do:

if (isset($_POST['submitButton'])) { 
//do something

//..do all post stuff
header('Location: thisPage.php'); //clears POST
}


<form action = "table.php" method="post">
<label for="submitButton"></label>
<input type="submit" name="submitButton" id="submitButton"
value="Submit Form"/>
</form>



回答2:


I use a session to keep from reposting.

session_start();

 if( isset($_SESSION['your_variable']) && 
     $_SESSION['your_variable'] == $_POST['your_variable'] ){
    // re-post, don't do anything. 
 }
 else{
    $_SESSION['your_variable'] = $_POST['your_variable'];
    // new post, go do something.
 } 



回答3:


This is a standard behavior : when you reload the page, if it was posted, your browser replays the same request (with the POST).

To avoid this, you can use a redirection to the same page, with :

 <?php
 header("location:".$mycurrentURl);

This will reload the page, via a get request. This will prevent double posts.




回答4:


when you refresh the page. browser post all the data again. so the same thing happens again to overcome this after doing something redirect the browser to same page again once like this

    if (isset($_POST['submitButton'])) { 
         //do something

         header("location:table.php");
    }



回答5:


I usually don't worry about this and just rely on the user NOT re-posting unless they want to. However, if you want to forbid it, you can use a nonce.

http://en.wikipedia.org/wiki/Cryptographic_nonce



来源:https://stackoverflow.com/questions/7082063/double-submission-when-refresh-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!