
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
How to prevent multiple inserts when submitting a form in PHP?
PHP session can be used to prevent multiple inserts when submitting a form. PHP session sets a session variable (say $_SESSION['posttimer']) that sets the current timestamp on POST. Before processing the form in PHP, the $_SESSION['posttimer'] variable is checked for its existence and checked for a specific timestamp difference (say 2 or 3 seconds). This way, those insertions that are actually duplicates can be identified and removed.
Simple form −
// form.html <form action="my_session_file.php" method="post"> <input type="text" name="bar" /> <input type="submit" value="Save"> </form>
The reference to ‘my_session_file.php’ in the above will have the below lines of code −
Example
if (isset($_POST) && !empty($_POST)) { if (isset($_SESSION['posttimer'])) { if ( (time() - $_SESSION['posttimer']) <= 2) { // less then 2 seconds since last post } else { // more than 2 seconds since last post } } $_SESSION['posttimer'] = time(); }
Output
This will produce the following output −
The unique form submitted data.
The posttimer session variable is set and when there is a time difference of 2 seconds or less before the last POST operation, it can be removed. Otherwise it is stored. The time function is called and the value is assigned to the posttimer session variable.
- Related Articles
- How to prevent buttons from submitting forms in HTML?
- Multiple Inserts for a single column in MySQL?
- How to clear the form after submitting in Javascript without using reset?
- Resize image before submitting the form HTML5
- How to prevent form from being submitted?
- What are the ways of submitting a form in Selenium with python?
- How to fasten MySQL inserts?
- Perform multiple inserts with INSERT INTO SELECT and UNION in MySQL
- How to specify that the element must be filled out before submitting the form in HTML?
- How to create a form with multiple steps in JavaScript?
- How to prevent numbers being changed to exponential form in Python Matplotlib?
- How to prevent a dialog from closing when a button is clicked?
- How to specify whether the form-data should be encoded while submitting to the server with HTML?
- How to write a MySQL stored function that inserts values in a table?
- How to prevent a dialog from closing when a button is clicked using Kotlin?
