DeTechn Blog

php记录搜索引擎爬行记录的实现代码

<?php

/**
 * 获取搜索引擎爬行记录
 */
function get_naps_bot()
{
    $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
    if (strpos($useragent, 'googlebot') !== false) {
        return 'Google';
    }
    if (strpos($useragent, 'baiduspider') !== false) {
        return 'Baidu';
    }
    if (strpos($useragent, 'msnbot') !== false) {
        return 'Bing';
    }
    if (strpos($useragent, 'slurp') !== false) {
        return 'Yahoo';
    }
    if (strpos($useragent, 'sosospider') !== false) {
        return 'Soso';
    }
    if (strpos($useragent, 'sogou spider') !== false) {
        return 'Sogou';
    }
    if (strpos($useragent, 'yodaobot') !== false) {
        return 'Yodao';
    }

    return false;
}

function nowtime()
{
    $date = date('Y-m-d.G:i:s');

    return $date;
}

$searchbot = get_naps_bot();

var_dump($searchbot); die;
if ($searchbot) {
    $tlc_thispage = addslashes($_SERVER['HTTP_USER_AGENT']);
    $url = $_SERVER['HTTP_REFERER'];
    $file = 'www.txt';
    $time = nowtime();
    $data = fopen($file, 'a');
    fwrite($data, "Time:$time robot:$searchbot URL:$tlc_thispage\n");
    fclose($data);
}

//记录搜索引擎爬行记录
// $searchbot = get_naps_bot();

// if ($searchbot) {
//     $tlc_thispage = addslashes($_SERVER['HTTP_USER_AGENT']);
//     $url = $_SERVER['HTTP_REFERER'];
//     $file = WEB_PATH.'api/robotslogs.txt';
//     $date = date('Y-m-d H:i:s');
//     $data = fopen($file, 'a');
//     fwrite($data, "Time:$date robot:$searchbot URL:$tlc_thispage/r/n");
//     fclose($data);
// }

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »