设为首页 加入收藏

TOP

[PHP] 频率限制类(一)
2019-08-23 00:35:10 】 浏览:43
Tags:PHP 频率 限制

比如要实现

单个ip限制60秒1次
单个关键字,比如手机号,限制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;
    }
    /*验证限制规则*/
    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;
    }
    /*更新限制规则*/
    public function update($c = 1, $ip = null, $key = null) {
        if (is_null($ip) && is_null($key)) {
            if (!empty($this->updates)) {
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇微信js-sdk开发获取签名和获取地.. 下一篇PHP代码优化—array_push

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目