double submission when refresh php

戏子无情 提交于 2019-12-03 20:16:49

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>

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

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.

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

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

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