Maison > développement back-end > tutoriel php > Exemple d'utilisation de la classe de limite de fréquence php

Exemple d'utilisation de la classe de limite de fréquence php

不言
Libérer: 2023-04-05 18:34:02
avant
2978 Les gens l'ont consulté

Le contenu de cet article concerne les exemples d'utilisation de la classe de limite de fréquence PHP. Il a une certaine valeur de référence. Les amis dans le besoin peuvent s'y référer.

Par exemple :

Atteindre une limite d'adresse IP unique d'une fois toutes les 60 secondes

Un seul mot-clé, tel qu'un numéro de téléphone mobile, est limité à une fois toutes les 60 secondes, et 10 fois toutes les 3600 secondes

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

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

<?php

class Sina_Mail_WebAntispam {

 

    const PREFIX_WHITELIST = &#39;w:&#39;;

    const PREFIX_KILL = &#39;k:&#39;;

    const PREFIX_VERIFYCODE = &#39;c:&#39;;

    const PREFIX_VERIFIED = &#39;v:&#39;;

    const STATUS_UPDATE = &#39;[U]&#39;;

 

    private $mc = null;

    private $config = null;

    private $whitelist = array();

    private $keyPrefix = &#39;&#39;;

    private $intervals = array();

    private $updates = array();

    private $status = array();

 

    public function __construct($mc, $config) {

        $this->mc = $mc;

        $this->config = $config;

        if (isset($this->config->prefix)) {

            $this->keyPrefix = $this->config->prefix;

        }

        if (isset($this->config->whitelistKey)) {

            $wls = $this->mc->get($this->config->whitelistKey);

            if (!empty($wls)) {

                $this->whitelist = & $wls;

            }

        }

    }

 

    public function setWhitelist(&$whitelist) {

        $this->whitelist = & $whitelist;

    }

    /*验证限制规则*/

    public function check($ip = null, $key = null) {

        if (!$ip && !$key) {

            return false;

        }

 

        if ($key) {

            if (!is_array($key)) {

                $keys = array($key);

            } else {

                $keys = $key;

            }

        }

 

        // first filter by whitelist

        if (!empty($this->whitelist)) {

            if ($ip && $this->filterByWhitelist($ip, &#39;ip&#39;)) {

                $this->status[self::PREFIX_WHITELIST . $ip] = 1;

                return true;

            }

            if ($keys) {

                foreach ($keys as $key) {

                    if ($this->filterByWhitelist($key, &#39;key&#39;)) {

                        $this->status[self::PREFIX_WHITELIST . $key] = 1;

                        return true;

                    }

                }

            }

        }

 

        if ($ip) {

            $ip = $this->keyPrefix . $ip;

        }

 

        // second, check verified ok

        if (!empty($this->config->verified)) {

            if ($ip && $this->mc->get(self::PREFIX_VERIFIED . $ip)) {

                $this->status[self::PREFIX_VERIFIED . $ip] = 1;

                return true;

            }

            if ($keys) {

                foreach ($keys as $key) {

                    $verifiedKey = self::PREFIX_VERIFIED . $this->keyPrefix . $key;

                    if ($this->mc->get($verifiedKey)) {

                        $this->status[$verifiedKey] = 1;

                        return true;

                    }

                }

            }

        }

 

        $kos = !empty($this->config->kill);

        // check killed

        if ($kos) {

            if ($ip && $this->mc->get(self::PREFIX_KILL . $ip)) {

                $this->status[self::PREFIX_KILL . $ip] = 1;

                return false;

            }

            if ($keys) {

                foreach ($keys as $key) {

                    $killKey = self::PREFIX_KILL . $this->keyPrefix . $key;

                    if ($this->mc->get($killKey)) {

                        $this->status[$killKey] = 1;

                        return false;

                    }

                }

            }

        }

 

        // check ip rule

        if ($ip && isset($this->config->ip)) {

            if (!$this->checkRule($ip, $this->config->ip)) {

                if ($kos && $this->mc->set(self::PREFIX_KILL . $ip, 1, intval($this->config->kill))) {

                    $this->status[self::PREFIX_KILL . $ip] = 1;

                }

                return false;

            }

        }

 

        // check keys rule

        if ($keys && isset($this->config->key)) {

            foreach ($keys as $key) {

                if (!$this->checkRule($this->keyPrefix . $key, $this->config->key)) {

                    $killKey = self::PREFIX_KILL . $this->keyPrefix . $key;

                    if ($kos && $this->mc->set($killKey, 1, intval($this->config->kill))) {

                        $this->status[$killKey] = 1;

                    }

                    return false;

                }

            }

        }

 

        return true;

    }

    /*更新限制规则*/

    public function update($c = 1, $ip = null, $key = null) {

        if (is_null($ip) && is_null($key)) {

            if (!empty($this->updates)) {

                foreach ($this->updates as $k => $v) {

                    if (!$v && isset($this->intervals[$k])) {

                        if ($this->mc->add($k, $c, false,$this->intervals[$k])) {

                            $this->status[self::STATUS_UPDATE . $k] = $c;

                            continue;

                        }

                    }

                    $r = $this->mc->increment($k, $c);

                    $this->status[self::STATUS_UPDATE . $k] = $r;

                }

            }

        } else {

            if (!is_null($ip) && isset($this->config->ip)) {

                $rule = $this->config->ip;

                foreach ($rule as $interval => $limit) {

                    $k = $this->keyPrefix . $ip . &#39;_&#39; . $interval;

                    if ($this->mc->add($k, $c,false,$interval)) {

                        $this->status[self::STATUS_UPDATE . $k] = true;

                        continue;

                    }

                    $r = $this->mc->increment($k, $c);

                    $this->status[self::STATUS_UPDATE . $k] = $r;

                }

            }

            if (!is_null($key) && isset($this->config->key)) {

                $rule = $this->config->key;

                if (!is_array($key)) {

                    $keys = array($key);

                } else {

                    $keys = $key;

                }

                foreach ($keys as $key) {

                    foreach ($rule as $interval => $limit) {

                        $k = $this->keyPrefix . $key . &#39;_&#39; . $interval;

                        if ($this->mc->add($k, $c,false,$interval)) {

                            $this->status[self::STATUS_UPDATE . $k] = true;

                            continue;

                        }

                        $r = $this->mc->increment($k, $c);

                        $this->status[self::STATUS_UPDATE . $k] = $r;

                    }

                }

            }

        }

    }

 

    public function checkVerifyCode($key, $code) {

        $servcode = $this->mc->get(self::PREFIX_VERIFYCODE . $this->keyPrefix . $key);

        if (strcasecmp($servcode, $code) == 0) {

            $verified = intval($this->config->verified);

            if ($verified > 0) {

                $r = $this->mc->set(self::PREFIX_VERIFIED . $this->keyPrefix . $key, 1, false, $verified);

            } else {

                $r = true;

            }

            if ($r) {

                $this->mc->delete(self::PREFIX_VERIFYCODE . $this->keyPrefix . $key);

            }

            return $r;

        }

        return false;

    }

 

    public function isVerified($key) {

        $r = $this->mc->get(self::PREFIX_VERIFIED . $this->keyPrefix . $key);

        if (!empty($r)) {

            return true;

        } else {

            return false;

        }

    }

 

    public function setVerifyCode($key, $code) {

        $verifytime = intval($this->config->verifytime);

        if ($verifytime < 1) {

            return false;

        }

        return $this->mc->set(self::PREFIX_VERIFYCODE . $this->keyPrefix . $key, $code, false, $verifytime);

    }

 

    public function getStatus() {

        return $this->status;

    }

 

    private function filterByWhitelist($value, $key) {

//        if (empty($this->whitelist[$key])) {

//            return false;

//        }

//        $ls = & $this->whitelist[$key];

        $ls = & $this->whitelist;

        foreach ($ls as $i) {

            if ($i[strlen($i) - 1] == &#39;.&#39;) { // ip segment

                if (strpos($value, $i) === 0) {

                    return true;

                }

            } else {

                if (strcmp($i, $value) === 0) {

                    return true;

                }

            }

        }

        return false;

    }

 

    private function checkRule($key, $rule) {

        $flag = true;

        if (!empty($rule)) {

            foreach ($rule as $interval => $limit) {

                $k = $key . &#39;_&#39; . $interval;

                $c = $this->mc->get($k);

                if (!$c) {

                    $this->updates[$k] = 0;

                    $this->intervals[$k] = $interval;

                    $this->status[$k] = 0;

                } else {

                    $this->updates[$k] = $c;

                    $this->status[$k] = $c;

                    if ($c >= $limit) {

                        $flag = false;

                    }

                }

            }

        }

        return $flag;

    }

 

    public static function getInstance($conf) {

        $mc = new Memcache();

        $mc->connect("115.159.28.112");

        $conf=json_decode(json_encode($conf));

        return new self($mc, $conf);

    }

 

}

/*

单个ip限制60秒1次

单个关键字,比如手机号,限制60秒1次,3600秒10次

*/

$conf=array(

            &#39;prefix&#39; => &#39;selfservice:&#39;,

            &#39;key&#39; => array(60 => 1,3600=>10),

            &#39;ip&#39; => array(60 => 1),

        );

$spam=Sina_Mail_WebAntispam::getInstance($conf);

if(!$spam->check(&#39;127.25.12.123&#39;,17610725730)){

    echo "limit...";

    exit;

}

 

//更新频率限制

$spam->update();

Copier après la connexion

La clé de stockage finale dans memache

[Recommandé cours : Tutoriel vidéo PHP

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
php
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Derniers numéros
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal