PHP Summary of Several Common Methods to Prevent Repeated Submission of Forms

  • 2021-07-10 19:18:18
  • OfStack

This paper summarizes several common methods of PHP to prevent repeated submission of forms in detail, which has high practical value in PHP program development. The specific methods are as follows:

1. Use JS to disable the button after one click (disable). This method can prevent the occurrence of multiple clicks, and the implementation mode is simple.

The disadvantage is that if the client disables JavaScript script, it will fail.

2. Perform page redirection (redirect) after successful submission. Go to the Submit Success Information page.

Features: Avoid F5 repeated submissions and eliminate the same problems that browser forward and backward buttons can cause.

3. The form hidden field holds session (the tag generated when the form is requested). After receiving the form data, check whether the flag value exists, delete it first, and then process the data; If it does not exist, it means it has been submitted, and this submission is ignored.


/*
// The server generates random numbers and stores them session,  Assign to a form page 
$data['sess_id'] = $_SESSION['sid'] = mt_rand(1000, 9999);
$this->load->view('form', $data);

// The form page hidden field holds this session Value 
<input type="hidden" name="sid" value="<?=$sess_id; ?>">

// Deal with 
if($_POST['sid'] != '' && $_POST['sid'] == $_SESSION['sid'])
{
   unset($_SESSION['sid']);

   echo ' Processing data ';
}
else
{
   echo ' The form has been submitted ';
}

4. The database has only one index constraint (the most effective way to prevent duplicate data).

I hope the method described in this paper can play a helpful role in the development of PHP project.


Related articles: