插件窝 干货文章 php 限制每分钟发送的短信条数(请求限流)

php 限制每分钟发送的短信条数(请求限流)

php 短信 限流 1340    来源:插件窝    2019-11-21
public function limitGet(){
   $mobile = input('mobile');
   dump(count(cache(date('i'))));
   if(cache(date('i')) && count(cache(date('i'))) >= 10){//每分钟限制请求10次
      return submitJson(false,'','网络繁忙请重试');
   }else{
      if(true){//假设发送短信成功
      $arr = cache(date('i')) ? cache(date('i')) : [];
      $arr[] = $mobile;
      cache(date('i'),$arr,60);
      //cache($mobiel,短信码);
      return submitJson(false,'','发送短信成功');
   }else{
      return submitJson(false,'','发送短信失败');
   }
}

demo访问如下:image.png



ps:如果是php请求频率限流的话,把$mobile改成获取ip()即可,道理是一样的 关键是判断cache(date('i'))