zoukankan      html  css  js  c++  java
  • discuz3.0以下版本防采集设置

    /**
     * 防采集设置
     * @global type $_G
     * @return boolean
     */
    function antitheft_control() {
        global $_G;
        //是否允许使用memcache
        $allowmem = memory('check');
        if(empty($allowmem)) {
            return true;
        }
        
        $lifetime = 86400; // memcache的生命周期
        $pervisittimes = 100; // 在单位时间内最多访问多少次
        $limitimes = 86400; //访问超过次数限制,限制多久不可以访问,前提是:memcache没有失效,所以memcache的生命周期不要太小于限制访问时间
        $intervaltime = 60; //单位时间;单位时间内访问次数超过限制次数,则禁止访问一段时间
        $antitheft = array(
            'white' => array(
                'single' => array('2130706433'),
                'range' => array(
                    array(
                        'min' => '',
                        'max' => ''
                    ),
                ),
            ),
            'black' => array(
                'single' => array(),
                'range' => array(
                    array(
                        'min' => '',
                        'max' => ''
                    ),
                ),
            ),
        );
        $ip = ip2long($_G['clientip']);
        if (!$ip || $ip == -1)
            showmessage('invalid request');
        if ($ip < 0) {
            $ip = sprintf('%u', $ip);
        }
        //此列出来的ip不受限制
        if (isset($antitheft['white'])) {
            if (in_array($ip, $antitheft['white']['single'])) {
                return true;
            }
            foreach ($antitheft['white']['range'] as $_ip) {
                if ($ip > $_ip['min'] && $ip < $_ip['max'])
                    return true;
            }
        }
        //此ip不可以访问
        if (isset($antitheft['black'])) {
            if (in_array($ip, (array) $antitheft['black']['single'], true)) {
                showmessage('have no right to visit again');
            }
            foreach ($antitheft['black']['range'] as $_ip) {
                if ($ip > $_ip['min'] && $ip < $_ip['max']) {
                    showmessage('have no right to visit again');
                }
            }
        }
        
        if (!($numarr = memory('get', $ip, 'iplimit'))) {
            $tmpval = json_encode(array(1, TIMESTAMP));
            memory('set', $ip, $tmpval, $lifetime, 'iplimit');
        } else {
            $numarr = json_decode($numarr, true);
            if (TIMESTAMP - $numarr[1] > $lifetime) {// 是否超过memcache的生命周期,如果是,从1开始计数
                $tmpval = json_encode(array(1, TIMESTAMP));
                memory('set', $ip, $tmpval, $lifetime, 'iplimit');
            } elseif ((TIMESTAMP - $numarr[1] > $intervaltime) && $numarr[0] > $pervisittimes) {//单位时间内超过了访问次数
                $tmpval = json_encode(array($pervisittimes + 1, $numarr[1]));
                memory('set', $ip, $tmpval, $limitimes, 'iplimit');
                showmessage('have no right to visit again');
            } else {//计数,更新memcache
                $tmpval = json_encode(array($numarr[0] + 1, $numarr[1]));
                memory('set', $ip, $tmpval, $lifetime, 'iplimit');
            }
        }
    }
  • 相关阅读:
    MyCLI :一个支持自动补全和语法高亮的 MySQL/MariaDB 客户端
    pathlib:优雅的路径处理库
    MySQL索引连环18问
    Mysql 百万级数据迁移实战笔记
    强大的Json解析工具 Jsonpath 实战教程
    JavaScript 中的 Var,Let 和 Const 有什么区别
    【前端安全】从需求分析开始,详解前端加密与验签实践
    vue3开发企业级生鲜系统项目
    mysql随笔
    shiro相关Filter
  • 原文地址:https://www.cnblogs.com/bandbandme/p/3089195.html
Copyright © 2011-2022 走看看