Home>

Duplicate submissions are a common problem encountered in our development.Except that we use js to prevent duplicate submission of the form,You can also use PHP to prevent duplicate submissions.

<?php
/*
 * How to prevent duplicate submission of forms in php
 * /
session_start ();
if (empty ($_ session ["ip"])) {//The first write operation,Determine whether the IP address is recorded, so as to know whether to write to the database
  $_session ["ip"]=$_server ["remote_addr"];//First write,Set the stage for future refresh or backward judgments
  //...........//Write to the database
} else {//There are already operations after the first write,No longer write to the database
  echo "Please do not refresh and back again";//write some hints or other things that have been written
}
?>

Specific principleSession scope variable token to prevent.

1. Open the session:session_start ();

2. If there is a form submission

if (isset ($token))

The token is contained in the form in the form of hidden.

<input type="hidden" name="token" value="?php echo $token;

3. If the form is submitted repeatedly

if ($_session ["token"]!=$token) {
  //Do not allow duplicate submissions,Handle here
  //header ("location:". $_ server ["php_self"]);
} else {
  //normal form submission,Handle here
  //echo "committed";
}

4. Set the token value

$token=mt_rand (0,1000000);

2 $_session ["token"]=$token;

php
  • Previous Ajax upload video with progress bar asynchronously and extract thumbnails
  • Next "Problem with the SSL CA cert" solution appears in WordPress upgrade version and installation plugin