Home  >  Article  >  Backend Development  >  How to implement anti-shake function in PHP to avoid repeated submissions

How to implement anti-shake function in PHP to avoid repeated submissions

王林
王林Original
2023-10-12 09:36:24823browse

如何在 PHP 中实现防抖功能,避免重复提交

How to implement the anti-shake function in PHP to avoid repeated submissions

The anti-shake function means that when the user continuously triggers an event, the event processing function is only executed once , and will not be executed again for a period of time after the last trigger. In actual development, users often encounter situations where users submit forms repeatedly. Using the anti-shake function can effectively avoid this situation.

There are many ways to implement anti-shake function in PHP. Below I will introduce a common implementation method and give specific code examples.

  1. Use Session to record the timestamp of the last submission.
// 开启 Session
session_start();

// 获取当前时间戳
$currentTimestamp = time();

// 获取上一次提交的时间戳
$lastTimestamp = $_SESSION['lastTimestamp'] ?? 0;

// 判断时间间隔是否小于指定的防抖时间(例如5秒)
if ($currentTimestamp - $lastTimestamp < 5) {
    echo '请勿重复提交';
    exit;
}

// 更新上一次提交的时间戳
$_SESSION['lastTimestamp'] = $currentTimestamp;

In this example, we use Session to record the timestamp of the last commit. By comparing the current timestamp with the timestamp of the last submission, determine whether the time interval is less than the specified anti-shake time (for example, 5 seconds). If the time interval is less than the anti-shake time, it can be considered as a repeated submission, and the prompt information will be output directly and the script execution will end.

It should be noted that in order to be able to use Session, we need to call the session_start() function at the beginning of the script to open the Session.

  1. Use Token to prevent repeated submissions.

Another commonly used way to prevent repeated submissions is to use Token. Each time the form is submitted, a unique Token is generated and saved in the Session or form hidden field. When processing form submission in the background, first verify whether the Token is valid, and if so, continue processing, otherwise a prompt message will be output.

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

// 开启 Session
session_start();

// 生成一个唯一的 Token
$token = md5(uniqid(rand(), true));

// 将 Token 保存在 Session 中
$_SESSION['token'] = $token;

// 在表单中输出隐藏域,将 Token 传递给后台
echo '<input type="hidden" name="token" value="' . $token . '">';

// 处理表单提交
if ($_POST['token'] !== $_SESSION['token']) {
    echo '请勿重复提交';
    exit;
}

// 继续处理表单数据
// ...

In this example, we generate a unique Token each time the form is submitted and save the Token in the Session middle. Pass the Token to the backend through hidden fields in the front-end form. When processing form submission in the background, first verify whether the Token obtained from the form is equal to the Token saved in the Session. If they are not equal, it will be considered a repeated submission, and the prompt information will be output directly and the script execution will end.

To sum up, by using Session or Token, we can implement the anti-shake function in PHP to avoid repeated submissions. According to the specific needs and scenarios, choose the appropriate method to implement. The above are two commonly used implementation methods, and specific code examples are given for reference. I hope to be helpful.

The above is the detailed content of How to implement anti-shake function in PHP to avoid repeated submissions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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