Home Backend Development PHP Tutorial PHP anti-shake and anti-duplicate submission technology: how to choose the right solution

PHP anti-shake and anti-duplicate submission technology: how to choose the right solution

Oct 12, 2023 am 09:54 AM
Anti-shake (debounce) prevent duplicate submissions Technology selection (choosing the right approach)

PHP 防抖和防重复提交技术:如何选择合适的方案

PHP anti-shake and anti-duplicate submission technology: How to choose the appropriate solution requires specific code examples

In web development, anti-shake and anti-duplicate submission are two different things a common technical requirement. When dealing with scenarios such as user interaction or form submission, we need to ensure that users can only trigger one request within a certain period of time to avoid repeated submissions or frequent requests. This article discusses how to choose the right solution and provides specific PHP code examples.

1. Anti-shake technology

Anti-shake technology means that when an event is triggered continuously, only the last operation is performed and the intermediate operations are ignored. Usually we will set a timer. If multiple events occur within a specified time, only the last event will be retained to ensure that the request is only sent once.

You can implement anti-shake in different ways, such as using JavaScript or PHP. The following is a sample code for anti-shaking implemented using PHP:

<?php
  
class Debounce
{
    private $callback;
    private $delay;
    private $timer;
  
    public function __construct($callback, $delay)
    {
        $this->callback = $callback;
        $this->delay = $delay;
    }
  
    public function debounce()
    {
        if($this->timer) {
            clearTimeout($this->timer);
        }
  
        $this->timer = setTimeout($this->callback, $this->delay);
    }
}
  
// 示例用法
$debounce = new Debounce(function() {
    // 执行请求的操作
}, 1000);
  
// 触发事件时调用 debounce() 方法
$debounce->debounce();
?>
Copy after login

2. Anti-repeated submission technology

The anti-repeated submission technology is to prevent users from submitting the same form or the same request multiple times. . A common solution is to ensure that the same form request is only processed once by adding a unique token to the form and validating it on the server side.

The following is a sample code that uses PHP to prevent repeated submissions:

<?php
  
class FormToken
{
    private $token;
  
    public function __construct()
    {
        if(!isset($_SESSION['token'])) {
            $_SESSION['token'] = bin2hex(random_bytes(32));
        }
  
        $this->token = $_SESSION['token'];
    }
  
    public function generateToken()
    {
        return '<input type="hidden" name="token" value="' . $this->token . '">';
    }
  
    public function validateToken()
    {
        if(isset($_POST['token']) && $_POST['token'] === $this->token) {
            // 执行表单提交的操作
        } else {
            // 非法请求,给出错误提示
        }
    }
}
  
// 示例用法
$formToken = new FormToken();
echo $formToken->generateToken();
$formToken->validateToken();
?>
Copy after login

Using this sample code, we can add a hidden token field to the form and perform it when the form is submitted. check. Only requests that pass verification will be processed.

3. Choose the appropriate solution

When choosing an anti-shake and anti-repetition submission solution, it needs to be evaluated based on specific business needs. The following points can be used as a reference:

  1. Anti-shake technology is suitable for situations where users frequently trigger the same event, such as continuous clicks of buttons, scrolling events, etc. Suitable for client-side interactive operations, which can reduce unnecessary requests.
  2. Anti-repeated submission technology is suitable for situations where it is necessary to prevent users from submitting the same form or request multiple times. Suitable for server-side form submission, data processing and other scenarios.
  3. If sensitive operations such as confidentiality are involved, it is recommended to use anti-shake and anti-re-submission technologies at the same time to improve security.

In short, whether it is anti-shaking or anti-duplicate submission, choosing the appropriate solution needs to be measured according to specific business needs. The above sample code can be used as a reference, and developers can make appropriate modifications and optimizations according to the actual situation.

The above is the detailed content of PHP anti-shake and anti-duplicate submission technology: how to choose the right solution. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

Announcement of 2025 PHP Situation Survey

See all articles