限制验证码获取次数,超过次数在某个时间段内禁止获取
//获取今天的发送次数
$times = Cache::get($times_key) ? :0;
if ($times > 3){
throw new Exception('今天发送次数过多');
}
//发送后次数加1
$times = $times + 1;
Cache::set($times_key,$times,30);
一天只能发布3条求租信息
//获取用户今天发送了都少次
$times = Cache::get('times');
//判断次数
if ($times > 3){
return json(['code' => 500,'msg' => '单用户一天只能发送3条求租信息','data' => []]);
}
if (empty($times)){
$expr = strtotime(date('Y-m-d',strtotime("+1 day"))) - time();
Cache::set('times',1,$expr);
}else{
$expr = strtotime(date('Y-m-d',strtotime("+1 day"))) - time();
$times +=1;
Cache::set('times',$times,$expr);
}
重复提交(后端)
//限制用户30秒内只能发送一次
//获取第一次发送的信息
$send_time = Cache::get('time_key');
//判断是否发送过
if (time() - $send_time < 30){
return json(['code' => 500,'msg'=> '30秒内只能发送一次','data' => []]);
}
if (empty($send_time)){
$time = time();
Cache::set('time_key',$time,30);
}
按钮禁用(前端)
//禁用按钮
var that = $(this);
that.attr('disabled','disabled');
var seconds = 60;
seconds--;
var timer = setInterval(function () {
if (seconds > 0){
that.text(seconds + "秒后重试");
seconds--;
}else{
clearInterval(timer);
that.text('发送验证码');
that.removeAttr('disabled');
}
},1000)