thinkphp5次数限制 用于限制一天内有N次机会,或X秒内有N次机会

3 篇文章 0 订阅
3 篇文章 1 订阅

** 用途:

    密码输入错误N次后锁住
    每天免费抽(红包)奖N此
    每小时最多评论N条,每小时最多发帖N条
    复杂接口频繁调用限制(每分钟最多调用N次)

**

/**
	 * 次数限制 用于限制一天内有N次机会,或X秒内有N次机会  优化后使用加法,逻辑更加通畅
	 * @param $unique_id 唯一标识(用户ID或手机号)
	 * @param $numberoftimes  最大次数N
	 * @param $timespant 时间间隔(字符串oneday一天或者过期秒数X)
	 * @param $verification 是否只做验证false|true
	 * @return bool|mixed
	 */
	function limitoftimes($unique_id,$numberoftimes=10,$timespant="oneday",$verification=false){
		$cacheneme = request()->url().$numberoftimes.'-'.$unique_id;
		$nowtimes = cache($cacheneme);//获取缓存中的当前次数
		if($nowtimes>=$numberoftimes)return false;
		if($verification)return true;
		if($timespant==="oneday"){
			$expires_in = strtotime(date('Y-m-d',strtotime('+1 day'))) - time();
		}else{
			$expires_inname = $cacheneme."-".$timespant;//设置时间缓存名称
			$expires_intime = cache($expires_inname);
			if($expires_intime){
				$expires_in = $timespant-(request()->time() - $expires_intime);
			}else{
				$expires_in = $timespant;
				cache($expires_inname,request()->time(),$expires_in);
			}
		}
		$nowtimes = $nowtimes+1;//不可用++ 因为有false的情况
		cache($cacheneme,$nowtimes,$expires_in);
		return $numberoftimes-$nowtimes;
	}
//调用方法实例
//普通的次数限制直接调用
if(limitoftimes($user_id,3,60)===false)$this->error("检测到您存在恶意刷屏行为,屏蔽一分钟");


//密码验证调用:
//验证时可调用, 其中limitoftimes($user_id,3,"oneday",true)返回的类型有两种种true|false 其中false代表限期内次数用尽
	    if(limitoftimes($user_id,5,'oneday',true) === 0)$this->error("今日密码输入错误次数超过5次,余额支付已锁定,请明日再试!");
	    if ($this->auth->password != $this->auth->getEncryptPassword($password, $this->auth->salt)){
		    $nowtimes = limitoftimes($user_id,5);
		    if(!$nowtimes)$this->error("今日密码输入错误次数超过5次,余额支付已锁定,请明日再试!");
		    $this->error("密码不正确,今日还有{$nowtimes}次机会");
	    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对 ThinkPHP 5 的情况,我可以给出以下的思路: 1.获取已有控制器的类名和方法名。 ```php $controllerName = 'app\controller\YourController'; // 已有控制器的类名 $methodName = 'yourMethod'; // 已有控制器的方法名 ``` 2.使用反射机制获取已有控制器方法的参数列表。 ```php $reflectionMethod = new \ReflectionMethod($controllerName, $methodName); $params = []; foreach($reflectionMethod->getParameters() as $param) { if ($param->isDefaultValueAvailable()) { $params[] = $param->getDefaultValue(); } else { $params[] = null; } } ``` 3.使用反射机制获取已有控制器方法的注释,包括路由和参数注释。 ```php $docComment = $reflectionMethod->getDocComment(); preg_match('/@route\W+(.*)/', $docComment, $routeMatch); $route = isset($routeMatch[1]) ? $routeMatch[1] : ''; preg_match_all('/@param\W+(\w+)\W+(\$[a-zA-Z0-9_]+)/', $docComment, $paramMatches); $paramsDoc = []; foreach ($paramMatches[2] as $index => $paramName) { $paramsDoc[$paramName] = $paramMatches[1][$index]; } ``` 4.使用获取到的信息,动态生成新的控制器方法。 ```php $newMethodName = 'newMethod'; // 新的控制器方法名 $newControllerContent = <<<CONTROLLER <?php namespace app\controller; use think\Controller; class NewController extends Controller { /** * $docComment */ public function $newMethodName($paramsDoc) { // 在此处编写新的控制器方法逻辑 } } CONTROLLER; file_put_contents('app/controller/NewController.php', $newControllerContent); // 将新的控制器文件写入磁盘 ``` 需要注意的是,以上代码仅为思路示例,具体实现可能需要根据您的需求进行调整。同时,自动生成的代码需要您自己进行调试和测试,以确保代码的正确性和稳定性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值