本人测试系统: CentOS Linux 7.4.1708 (Core) Apache 2.4 PhP7.1function frequent ($seconds,$counting,$jump){ $redis=new Redis(); // Redis 实例 $redis->connect ('127.0.0.1',6379); //连接本地的 Redis 服务 session_start(); //初始SESSION $key=isset($_SESSION['r_key']) ? $_SESSION['r_key'] : null; //首次是空值 $check=$redis->exists ($key); //检查 $key 是否存在 if ($check){ //首次 不存在 $redis->incr ($key); //首次$key不存在初始数字值
本人测试系统: CentOS Linux 7.4.1708 (Core) Apache 2.4 PhP7.1<?php $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $key=get_real_ip(); //限制次数为10 $limit = 10; $check = $redis->exists($key); if($check){ $redis->incr($key); $count = $redis->get($key); if($count > $limit){ exit('请求太频繁,请稍后再试!'); } }else{ $redis->incr($key); //限制时间为60秒 $redis->expire($key,60); } $count = $redis->get($key); echo '第 '.$count.' 次请求';
Hawk
别固执的认为某事必须 !