加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 创业 > C语言 > 正文

60秒限制5次

发布时间:2020-12-15 03:29:10 所属栏目:C语言 来源:网络整理
导读:table class="text" tr class="li1" td class="ln"pre class="de1"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
<tr class="li1">
<td class="ln"><pre class="de1">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155

connect('127.0.0.1',6379); $redis->auth("php001"); //这个key记录该ip的访问次数 也可改成用户id //$key = 'userid_11100'; $key=get_real_ip(); //限制次数为5 $limit = 5; $check = $redis->exists($key); if($check){ ? $redis->incr($key); ? $count = $redis->get($key); ? if($count > 5){ ? ? exit('请求太频繁,请稍后再试!'); ? } }else{ ? $redis->incr($key); ? //限制时间为60秒 ? $redis->expire($key,60); } $count = $redis->get($key); echo '第 '.$count.' 次请求'; //获取客户端真实ip地址 function get_real_ip(){ ? static $realip; ? if(isset($_SERVER)){ ? ? if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){ ? ? ? $realip=$_SERVER['HTTP_X_FORWARDED_FOR']; ? ? }else if(isset($_SERVER['HTTP_CLIENT_IP'])){ ? ? ? $realip=$_SERVER['HTTP_CLIENT_IP']; ? ? }else{ ? ? ? $realip=$_SERVER['REMOTE_ADDR']; ? ? } ? }else{ ? ? if(getenv('HTTP_X_FORWARDED_FOR')){ ? ? ? $realip=getenv('HTTP_X_FORWARDED_FOR'); ? ? }else if(getenv('HTTP_CLIENT_IP')){ ? ? ? $realip=getenv('HTTP_CLIENT_IP'); ? ? }else{ ? ? ? $realip=getenv('REMOTE_ADDR'); ? ? } ? } ? return $realip; } ? /** ?* Redis锁操作类 ?* Date: ?2017-06-30 ?* Author: fdipzone ?* Ver: ?1.0 ?* ?* Func: ?* public lock ?获取锁 ?* public unlock 释放锁 ?* private connect 连接 ?*/ class RedisLock { // class start ? ? private $_config; ? private $_redis; ? ? /** ? ?* 初始化 ? ?* @param Array $config redis连接设定 ? ?*/ ? public function __construct($config=array()){ ? ? $this->_config = $config; ? ? $this->_redis = $this->connect(); ? } ? ? /** ? ?* 获取锁 ? ?* @param String $key ?锁标识 ? ?* @param Int ? $expire 锁过期时间 ? ?* @return Boolean ? ?*/ ? public function lock($key,$expire=5){ ? ? $is_lock = $this->_redis->setnx($key,time()+$expire); ? ? ? // 不能获取锁 ? ? if(!$is_lock){ ? ? ? ? // 判断锁是否过期 ? ? ? $lock_time = $this->_redis->get($key); ? ? ? ? // 锁已过期,删除锁,重新获取 ? ? ? if(time()>$lock_time){ ? ? ? ? $this->unlock($key); ? ? ? ? $is_lock = $this->_redis->setnx($key,time()+$expire); ? ? ? } ? ? } ? ? ? return $is_lock? true : false; ? } ? ? /** ? ?* 释放锁 ? ?* @param String $key 锁标识 ? ?* @return Boolean ? ?*/ ? public function unlock($key){ ? ? return $this->_redis->del($key); ? } ? ? /**https://mp.weixin.qq.com/s/6Qac5m9PIYIbt6JhSaFxNg ? ?* 创建redis连接 ? ?* @return Link ? ?*/ ? private function connect(){ ? ? try{ ? ? ? $redis = new Redis(); ? ? ? $redis->connect($this->_config['host'],$this->_config['port'],$this->_config['timeout'],$this->_config['reserved'],$this->_config['retry_interval']); ? ? ? if(empty($this->_config['auth'])){ ? ? ? ? $redis->auth($this->_config['auth']); ? ? ? } ? ? ? $redis->select($this->_config['index']); ? ? }catch(RedisException $e){ ? ? ? throw new Exception($e->getMessage()); ? ? ? return false; ? ? } ? ? return $redis; ? } ? } // class end $config = array( ? 'host' => 'localhost',? 'port' => 6379,? 'index' => 0,? 'auth' => '',? 'timeout' => 1,? 'reserved' => NULL,? 'retry_interval' => 100,); ? // 创建redislock对象 $oRedisLock = new RedisLock($config); ? // 定义锁标识 $key = 'mylock'; ? // 获取锁 $is_lock = $oRedisLock->lock($key,10); ? if($is_lock){ ? echo 'get lock success
'; ? echo 'do sth..
'; ? sleep(5); ? echo 'success
'; ? $oRedisLock->unlock($key); ? // 获取锁失败 }else{ ? echo 'request too frequently
'; }(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

<table class="text">

    推荐文章
      热点阅读