<script>
function is_crawler() {
$userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
$spiders = array(
'Googlebot', // Google 爬虫
'Baiduspider', // 百度爬虫
'Yahoo! Slurp', // 雅虎爬虫
'YodaoBot', // 有道爬虫
'msnbot' // Bing爬虫
// 更多爬虫关键字
);
foreach ($spiders as $spider) {
$spider = strtolower($spider);
if (strpos($userAgent, $spider) !== false) {
return true;
}
}
return false;
}
</script>
<?php if(is_crawler()) {
// 向搜索引擎显示的内容
} else {
// 向自然人显示的内容
} ?><?php if(is_crawler()) {
// 向搜索引擎显示的内容
} else {
// 向自然人显示的内容
} ?>这段向搜索引擎 和 自然访客怎么写,才能实现?

解决方案 »

  1.   

    怎么上边判断agent的代码是js的,不是浏览器或者爬虫直接提交http请求到php,php根据header决定怎么呈现内容吗?
      

  2.   


    噢噢,<script>是<?php的意思,凌乱了。区别就在于<head>里各种meta打给爬虫的时候齐全一点,keywords,description
      

  3.   

    1、is_crawler 函数是 php 的,应放在 php 标记中。而不是 script 标记中
    2、你可把 is_crawler 函数放在独立的文件中,在需要的地方引入进来
    is_crawler.php
    <?php
    function is_crawler() {
        $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
        $spiders = array(
            'Googlebot', // Google 爬虫
            'Baiduspider', // 百度爬虫
            'Yahoo! Slurp', // 雅虎爬虫
            'YodaoBot', // 有道爬虫
            'msnbot' // Bing爬虫
            // 更多爬虫关键字
        );
        foreach ($spiders as $spider) {
            $spider = strtolower($spider);
            if (strpos($userAgent, $spider) !== false) {
                return true;
            }
        }
        return false;
    }在需要时
    include_once 'is_crawler.php';例:
    <?php
    include_once 'is_crawler.php';
    if(is_crawler()) {
      include '给搜索引擎看的页面.php';
      exit;
    }
    // 正常的页面内容