Comparison of implementation methods of PHP anti-shake and anti-duplicate submission
When developing web applications, we often encounter situations that require anti-shake (Debounce) and Preventing Duplicate Form Submission. Anti-shake means that when the user frequently triggers an event, we want to only execute the last triggered action, while anti-repetitive submission means that when the user submits the form multiple times in a short period of time, we need to ensure that only one submission is processed. This article will focus on comparing and introducing several ways to achieve anti-shaking and anti-resubmission in PHP, and provide specific code examples.
function debounce($callback, $delay) { $timer = null; return function() use ($callback, $delay, &$timer) { if ($timer !== null) { clearTimeout($timer); } $timer = setTimeout($callback, $delay); }; } // 使用防抖函数处理表单提交事件 $debouncedHandler = debounce(function() { // 处理表单提交逻辑 }, 1000); // 绑定事件处理函数 if ($_SERVER['REQUEST_METHOD'] === 'POST') { $debouncedHandler(); }
2.1. Token verification
By using Token verification, we can generate a unique identifier for each form request and store the identifier in the session. When the user submits the form, we first check whether the identifier exists and whether it is consistent with the identifier in the request to determine whether it is a repeated submission. The following is a sample code that uses PHP to implement Token verification:
session_start(); function generateToken() { return md5(uniqid(rand(), true)); } function validateToken($token) { // 从 session 中获取 token $storedToken = $_SESSION['token']; return $storedToken && $token === $storedToken; } function removeToken() { // 从 session 中移除 token unset($_SESSION['token']); } // 生成 Token 并存储在 session 中 $_SESSION['token'] = generateToken(); // 处理表单提交逻辑 if ($_SERVER['REQUEST_METHOD'] === 'POST') { $token = $_POST['token']; if (validateToken($token)) { // 执行表单提交操作 // ... // 提交完成后移除 Token removeToken(); } else { // Token 验证失败,可能是重复提交 // 提示用户不要重复提交 } }
2.2. Redirection
Using redirection to handle page jumps after form submission can effectively prevent users from repeatedly submitting the form. When the user submits the form, we first handle the submission logic and then use the header function to redirect the user to a new page. The following is a sample code that uses PHP to implement redirection:
// 处理表单提交逻辑 if ($_SERVER['REQUEST_METHOD'] === 'POST') { // 执行表单提交操作 // ... // 重定向到新页面 header("Location: success.php"); exit; }
success.php
in the above code is a page that prompts the user to submit successfully, so that even if the user presses the refresh button, the Only the success.php
page will be refreshed and the form will not be submitted again.
Summary:
By comparing the implementation methods of PHP anti-shaking and anti-duplicate submission, we can choose the appropriate method to protect our application according to specific needs. Anti-shake is suitable for situations where users need to avoid triggering events frequently, while preventing repeated submission of forms is to prevent users from submitting forms multiple times. Using Token verification and redirection are two common and feasible methods. Developers can choose the appropriate method to prevent repeated submissions based on the actual situation.
The above is the detailed content of Comparison of implementation methods of PHP anti-shake and anti-duplicate submission. For more information, please follow other related articles on the PHP Chinese website!