未插入DB-bot/spider/爬网程序活动


Not inserting on DB bot/spider/crawlers activity

在我的网页上,我注册访问者的活动以进行分析。

然而,结果会受到机器人访问的影响。

我想知道在将数据保存到DB之前检查user_agent是否是一种明智的方法(请参阅底部的函数)。我担心我的网络负载太重。有很多机器人,我不知道在每次访问中,检查user_agent与30个机器人列表是否有效。

$bots = array( 'googlebot', 'msnbot', 'baidu', ... up to 30 );
$isRobot = false;
$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
foreach ( $bots as $bot ) {
        if ( strpos( $ua, $bot ) !== false )
            $isRobot = true;
        }
        if ( !$isRobot ) {
            // insert in the db
        }
    }

另一种选择是允许在DB上插入,然后删除它们。

使用in_array:

$bots = array( 'googlebot', 'msnbot', 'baidu', ... up to 30 );
$isRobot = false;
$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
if(in_array($ua, $bots)) {
    $isRobot = true;
}
if ( !$isRobot ) {
    // insert in the db
}