小編給大家分享一下php頻率限制類怎么用,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
站在用戶的角度思考問題,與客戶深入溝通,找到西林網(wǎng)站設(shè)計(jì)與西林網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名申請(qǐng)、網(wǎng)頁(yè)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋西林地區(qū)。
實(shí)現(xiàn)單個(gè)ip限制60秒1次
單個(gè)關(guān)鍵字,比如手機(jī)號(hào),限制60秒1次,3600秒10次
<?php class Sina_Mail_WebAntispam { const PREFIX_WHITELIST = 'w:'; const PREFIX_KILL = 'k:'; const PREFIX_VERIFYCODE = 'c:'; const PREFIX_VERIFIED = 'v:'; const STATUS_UPDATE = '[U]'; private $mc = null; private $config = null; private $whitelist = array(); private $keyPrefix = ''; 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; } /*驗(yàn)證限制規(guī)則*/ 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, 'ip')) { $this->status[self::PREFIX_WHITELIST . $ip] = 1; return true; } if ($keys) { foreach ($keys as $key) { if ($this->filterByWhitelist($key, 'key')) { $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; } /*更新限制規(guī)則*/ 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 . '_' . $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 . '_' . $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] == '.') { // 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 . '_' . $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); } } /* 單個(gè)ip限制60秒1次 單個(gè)關(guān)鍵字,比如手機(jī)號(hào),限制60秒1次,3600秒10次 */ $conf=array( 'prefix' => 'selfservice:', 'key' => array(60 => 1,3600=>10), 'ip' => array(60 => 1), ); $spam=Sina_Mail_WebAntispam::getInstance($conf); if(!$spam->check('127.25.12.123',17610725730)){ echo "limit..."; exit; } //更新頻率限制 $spam->update();
memache中最終的存儲(chǔ)key
看完了這篇文章,相信你對(duì)php頻率限制類怎么用有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
當(dāng)前文章:php頻率限制類怎么用
分享鏈接:http://chinadenli.net/article36/ihgepg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、小程序開發(fā)、網(wǎng)站改版、網(wǎng)站收錄、搜索引擎優(yōu)化、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)