如何确保提交按钮不被滥用以重复 运行 php 代码

How to make sure Submit button isn't abused to run php code repeatedly

<form action="test.php" method="POST">
HTML CODE
</form>
<button type="submit" name="test" value="1">Buy</button>

 ---> test.php

if(isset($_POST['test'])) {

PHP CODE

}

我在 html 表单上方添加了 test.php,但是当我在页面 运行 之前一遍又一遍地点击提交时 php 代码和再次加载整个页面,它 运行 是我点击 php 代码的次数

我如何确保不会一次又一次地滥用 'submit' 按钮到 运行 php 代码?我只想考虑第一次点击。

有人可以帮忙吗

最好的办法是加入某种验证码。我个人更喜欢来自 Google (https://www.google.com/recaptcha/intro/v3beta.html)

的 Recaptcha

它根本不显示,您可以为任何传入请求验证它。

要开始使用,请在您的 html 中嵌入以下 JS:

<script src='https://www.google.com/recaptcha/api.js'></script>

然后替换您的提交按钮:

<button
  class="g-recaptcha"
  data-sitekey="YOUR_PUBLIC_SITE_KEY"
  data-callback="YourOnSubmitFn">
Buy
</button>

剩下的最后一件事是从 php 检查代码是否正确:)

POST 'https://www.google.com/recaptcha/api/siteverify' with secret, response and remoteip -> 然后允许用户使用函数,否则就中止