/** * 防采集设置 * @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'); } } }