redis实现限制同一ip、一定时间内限制访问次数

//限制同一ip,60秒内访问次数为3次。
function getVisitCount() 
{
$redis=new Redis();
$key=get_client_ip();
$check = $redis->exists($key);  
if($check){  
    $redis->incr($key);  

    $count = $redis->get($key);  

    if($count > 3){  
        exit('已经超出了限制次数');  
                  }  
       }

    else
    {  
    $redis->incr($key);  
    //限制时间为60秒
    $redis->expire($key,60);  
    }
      //$count = $redis->get($key);  
      //echo '第 '.$count.' 次请求';

}

猜你喜欢

转载自blog.csdn.net/qq_39418742/article/details/102494390