The following situations will cause the form to be submitted repeatedly:
·Click the submit button twice.
·Click the refresh button.
·Use the browser back button to repeat the previous operation, resulting in repeated submission of the form.
·Use browser history to submit the form repeatedly.
· Repeated HTTP requests from the browser.
·The web page was maliciously refreshed.
The following are several solutions:
1. Use js to set the button to turn gray when clicked
<form name=form1 method=”POST” action=”/” target=_blank> <p> <input type=”text” name=”T1″ size=”20″> <input type=”button” value=”提交” οnclick=”javascript:{this.disabled=true;document.form1.submit();}”> </p> </form>
Click After the button is completed, it turns gray and cannot be clicked. If the user needs to submit the form again, he must refresh the page, fill in the data again, and then submit.
Related recommendations: "php tutorial"
2. Use session
to put a special mark in the session. When the form page is requested, a special character string is generated, stored in the session, and placed in the hidden field of the form. When accepting and processing form data, check whether the identification string exists, immediately delete it from the session, and then process the data normally.
If it is found that there is no valid flag string in the form submission, it means that the form has been submitted and the submission is ignored.
This gives your web application more advanced XSRF protection.
When loading the submitted page, a random number is generated:
$code = mt_rand(0,1000000);
Stored in the hidden input box of the form:
< input type=”hidden” name=”code” value=””>
The PHP code on the receiving page is as follows:
<?php session_start(); if(isset($_POST[‘code'])) { if($_POST[‘code'] == $_SESSION[‘code']){ // 重复提交表单了 }else{ $_SESSION[‘code'] =$_POST[‘code']; //存储code } }?>
3. Using cookies
The principle is similar to that of session, but once the user's browser disables cookies, this function will be invalid.
if(isset($_POST[‘submit'])){ setcookie(“tempcookie”,””,time()+30); header(“Location:”.$_SERVER[PHP_SELF]);exit(); } if(isset($_COOKIE[“tempcookie”])){ setcookie(“tempcookie”,””,0);echo “您已经提交过表单”; }
4. Use header function to jump
Once the user clicks the submit button, jump to other pages after processing the data.
if (isset($_POST[‘submit'])) { header(‘location:success.php');//处理数据后,转向到其他页面 }
5. Use the database to add constraints
Add unique constraints or create unique indexes directly in the database. Once the user is found to have submitted repeatedly, a warning will be thrown directly or Tip, or only process the data submitted for the first time. This is the most direct and effective method, and requires thorough consideration of the early database design and architecture.
6. Post/Redirect/Get mode
Perform page redirection after submission. This is the so-called Post-Redirect-Get (PRG) mode. In short, when the user submits the form, you perform a client-side redirection to the submission success information page.
if (isset($_POST[‘action']) && $_POST[‘action'] == ‘submitted') { //处理数据,如插入数据后,立即转向到其他页面 header('location:submits_success.php'); }
The above is the detailed content of How to avoid multiple submissions in php. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools