PHP反爬虫攻略禁止垃圾蜘蛛爬虫UserAgent频繁访问网站

时间:2020-06-05 15:05:42

我们都知道网络上的爬虫非常多,有对网站收录有益的,比如百度蜘蛛(Baiduspider),也有不但不遵守 robots 规则对服务器造成压力,还不能为网站带来流量的无用爬虫,比如宜搜蜘蛛(YisouSpider)(最新补充:宜搜蜘蛛已被 UC 神马搜索收购!所以本文已去掉宜搜蜘蛛的禁封!)。

PHP反爬虫攻略禁止垃圾蜘蛛爬虫UserAgent频繁访问网站

最近做了一个记录蜘蛛访问记录到数据库中插件出现了好多宜搜等垃圾的抓取记录,于是整理收集了网络上各种禁止垃圾蜘蛛爬站的方法,在给自己网做设置的同时,也给各位站长提供参考。?

PHP反爬虫攻略禁止垃圾蜘蛛爬虫UserAgent频繁访问网站

将如下方法放到贴到网站入口文件 index.php 中或者公共文件中 <?php 之后即可:

//屏蔽恶意网站抓取

$uagent = $_SERVER['HTTP_USER_AGENT'];

$now_ua = array(

    'FeedDemon ',

    'CrawlDaddy ',

    'Java',

    'Feedly',

    'UniversalFeedParser',

    'ApacheBench',

    'Swiftbot',

    'ZmEu',

    'IndyLibrary',

    'oBot',

    'jaunty',

    'YandexBot',

    'AhrefsBot',

    'MJ12bot',

    'WinHttp',

    'EasouSpider',

    'HttpClient',

    'Microsoft URL Control',

    'YYSpider','jaunty',

    'Python-urllib',

    'lightDeckReports Bot',

    'BLEXBot',

    'MJ12bot',

    'X11',

);

//禁止空USER_AGENT,dedecms等主流采集程序都是空USER_AGENT,部分sql注入工具也是空USER_AGENT

$userAgent =strtolower(@Request::instance()->header()['user-agent']);

if(empty($userAgent)){

header('HTTP/1.1 404 Not Found');

header("status: 404 Not Found");

echo '请求错误!';

exit();

}

$SpiderNo = array('feeddemon ','crawldaddy ','java','feedly','universalfeedparser','apachebench','swiftbot','zmeu','indylibrary','obot','jaunty','yandexbot','ahrefsbot','mj12bot','winhttp','easouspider','httpclient','microsoft url control','yyspider','jaunty','python-urllib','lightdeckreports','blexbot','mj12bot','x11');

//$SpiderNo = array('FeedDemon ','CrawlDaddy ','Java','Feedly','UniversalFeedParser','ApacheBench','Swiftbot','ZmEu','IndyLibrary','oBot','jaunty','YandexBot','AhrefsBot','MJ12bot','WinHttp','EasouSpider','HttpClient','Microsoft URL Control','YYSpider','jaunty','Python-urllib','lightDeckReports','BLEXBot','MJ12bot','X11');

foreach($SpiderNo as $value ){

        //判断是否是数组中存在的UA

        preg_match("/$value/i", $userAgent, $rarr); 

        if($rarr ) {

           header("Content-type: text/html; charset=utf-8");

           header('HTTP/1.1 404 Not Found');

          header("status: 404 Not Found");

         echo '请求错误!';

        exit;

        }

    }

附录(爬虫说明):

FeedDemon             内容采集

BOT/0.1 (BOT for JCE) sql注入

CrawlDaddy            sql注入

Java                  内容采集

Jullo                 内容采集

Feedly                内容采集

UniversalFeedParser   内容采集

ApacheBench           cc攻击器

Swiftbot              无用爬虫

YandexBot             无用爬虫

AhrefsBot             无用爬虫

YisouSpider           无用爬虫(已被UC神马搜索收购,此蜘蛛可以放开!)

MJ12bot               无用爬虫

ZmEu phpmyadmin       漏洞扫描

WinHttp               采集cc攻击

EasouSpider           无用爬虫

HttpClient            tcp攻击

Microsoft URL Control 扫描

YYSpider              无用爬虫

jaunty                wordpress爆破扫描器

oBot                  无用爬虫

Python-urllib         内容采集

Indy Library          扫描

FlightDeckReports Bot 无用爬虫

Linguee Bot           无用爬虫

猜你喜欢

版权所有:2019-2021 无双建设   网站地图