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');
        }
    }
}

 

转载于:https://www.cnblogs.com/bandbandme/archive/2013/05/20/3089195.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值