php - Can't submit multiple times within a fixed time?
仅有的幸福
仅有的幸福 2017-05-27 17:42:53
0
4
715

For example, if 5 tags are generated within one minute, it will prompt "Frequent operations, please enter the verification code".

How should we deal with it?

仅有的幸福
仅有的幸福

reply all(4)
PHPzhong

This is very convenient to implement using redis. A key is used to store the number of submissions. If the key is count.
Get count from redis every time you submit

  1. If count is empty, set the count value to 1, set the timeout to one minute, and submit normally;

  2. If the count value is not empty and is greater than or equal to 5, an error message will be reported "Operation is frequent, please enter the verification code";

  3. If the count value is not empty and less than 5, it will be submitted normally and the count value will be increased by one.

曾经蜡笔没有小新

Record the generation time of each tag
If the rule is that only 4 tags can be generated in one minute, an error will be reported for the 5th one
Just compare the 1st of the 4 most recent ones before adding the 5th one Is the generation time of each time, compared with the current time, greater than 60 seconds? If not, an error will be reported.

刘奇

Use session to record two values, one is the number of submissions $count and the other is the submission time $time. ++$count==5 [Condition 1] is detected every time it is submitted. If so, time()-$time>=60 seconds [Condition 2]. If both conditions are met, frequent operations will be prompted. If only the condition is met 1 then $count=0;$time=time();

世界只因有你

It is recommended to use redis cache operation

<?php
try {
  $redis = new Redis(); // 创建实例
  $redis->connect(REDIS_HOST, REDIS_PORT, REDIS_TIMEOUT); // 连接
  $redis->ping(); // 确认连接已经成功
} catch (Exception $e) {
  die('Can not connect Redis.');
}

$incrkey = 'TEST:用户:分钟'; // 每分钟缓存key
$incrValue = $redis->incr($incrkey);
if ($incrValue == 1) {
  // 设定缓存时间(键名,缓存时间[单位:秒])
  $redis->expire($incrkey, 60);
} else if ($incrValue >= 5) {
  die('操作频繁,请输入验证码');
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template