刷新php时双重提交

发布于 2024-11-29 14:47:40 字数 410 浏览 1 评论 0原文

我正在尝试纠正脚本中双重提交的问题。当我按下提交时,它只更新 mysql 一次(这就是我想要的)。但是,当我点击刷新时,它会再次更新 mysql。一旦点击刷新按钮,它似乎就会忽略 if 语句。我该怎么做才能阻止这个,

这是我的代码

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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦纸 2024-12-06 14:47:40

当您刷新页面时 - POST ISENT AGAIN

有些浏览器实际上会警告您发生这种情况。

为了防止这种情况,我这样做:

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>

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>
若相惜即相离 2024-12-06 14:47:40

我使用会话来防止重新发布。

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

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.
 } 
半仙 2024-12-06 14:47:40

这是标准行为:当您重新加载页面时,如果该页面已发布,您的浏览器将重放相同的请求(使用 POST)。

为了避免这种情况,您可以使用重定向到同一页面:

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

这将通过 get 请求重新加载页面。这将防止双重帖子。

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.

一杯敬自由 2024-12-06 14:47:40

当你刷新页面时。浏览器再次发布所有数据。因此,在执行某些操作将浏览器再次重定向到同一页面后,会再次发生同样的事情来克服此问题,如下所示

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

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

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");
    }
屌丝范 2024-12-06 14:47:40

我通常不担心这一点,只是依靠用户不会重新发布,除非他们愿意。但是,如果您想禁止它,可以使用随机数

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文