ecshop 蜘蛛爬行记录的实现

效果
http://www.hongyun2000.com/bot.php

实现步骤:
1.添加数据库表

Sql代码
  1. -- --------------------------------------------------------   
  2.   
  3.   
  4.   
  5.   
  6.   
  7. --   
  8.   
  9.   
  10. -- 蜘蛛表的结构 `ecs_bot`   
  11.   
  12.   
  13. --   
  14.   
  15.   
  16.   
  17.   
  18.   
  19. CREATE   TABLE  IF  NOT  EXISTS `ecs_bot` (  
  20.   
  21.   
  22.   `id` int (11)  NOT   NULL  AUTO_INCREMENT,  
  23.   
  24.   
  25.   `url` varchar (255)  DEFAULT   NULL ,  
  26.   
  27.   
  28.   `bot` varchar (20)  DEFAULT   NULL ,  
  29.   
  30.   
  31.   `timevarchar (30)  DEFAULT   NULL ,  
  32.   
  33.   
  34.   `ip` varchar (50)  DEFAULT   NULL ,  
  35.   
  36.   
  37.   PRIMARY   KEY  (`id`)  
  38.   
  39.   
  40. ) ENGINE=MyISAM DEFAULT  CHARSET=utf8 AUTO_INCREMENT=1 ;  
-- --------------------------------------------------------





--


-- 蜘蛛表的结构 `ecs_bot`


--





CREATE TABLE IF NOT EXISTS `ecs_bot` (


  `id` int(11) NOT NULL AUTO_INCREMENT,


  `url` varchar(255) DEFAULT NULL,


  `bot` varchar(20) DEFAULT NULL,


  `time` varchar(30) DEFAULT NULL,


  `ip` varchar(50) DEFAULT NULL,


  PRIMARY KEY (`id`)


) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;


 2.修改lib_main.php里的is_spider()

Php代码
  1. /**  
  2.  
  3.  
  4.  * 判断是否为搜索引擎蜘蛛  
  5.  
  6.  
  7.  *  
  8.  
  9.  
  10.  * @access  public  
  11.  
  12.  
  13.  * @return  string  
  14.  
  15.  
  16.  */   
  17.   
  18.   
  19. function  is_spider( $record  = true)  
  20.   
  21.   
  22. {  
  23.   
  24.   
  25.     static   $spider  = NULL;  
  26.   
  27.   
  28.   
  29.   
  30.   
  31.     if  ( $spider  !== NULL)  
  32.   
  33.   
  34.     {  
  35.   
  36.   
  37.         return   $spider ;  
  38.   
  39.   
  40.     }  
  41.   
  42.   
  43.   
  44.   
  45.   
  46.     if  ( empty empty ( $_SERVER [ 'HTTP_USER_AGENT' ]))  
  47.   
  48.   
  49.     {  
  50.   
  51.   
  52.         $spider  =  '' ;  
  53.   
  54.   
  55.   
  56.   
  57.   
  58.         return   '' ;  
  59.   
  60.   
  61.     }  
  62.   
  63.   
  64.   
  65.   
  66.   
  67.     $searchengine_bot  =  array (  
  68.   
  69.   
  70.         'googlebot' ,  
  71.   
  72.   
  73.         'mediapartners-google' ,  
  74.   
  75.   
  76.         'baiduspider+' ,  
  77.   
  78.   
  79.         'msnbot' ,  
  80.   
  81.   
  82.         'yodaobot' ,  
  83.   
  84.   
  85.         'yahoo! slurp;' ,  
  86.   
  87.   
  88.         'yahoo! slurp china;' ,  
  89.   
  90.   
  91.         'iaskspider' ,  
  92.   
  93.   
  94.         'sogou web spider' ,  
  95.   
  96.   
  97.         'sogou push spider'   
  98.   
  99.   
  100.     );  
  101.   
  102.   
  103.   
  104.   
  105.   
  106.     $searchengine_name  =  array (  
  107.   
  108.   
  109.         'GOOGLE' ,  
  110.   
  111.   
  112.         'GOOGLE ADSENSE' ,  
  113.   
  114.   
  115.         'BAIDU' ,  
  116.   
  117.   
  118.         'MSN' ,  
  119.   
  120.   
  121.         'YODAO' ,  
  122.   
  123.   
  124.         'YAHOO' ,  
  125.   
  126.   
  127.         'Yahoo China' ,  
  128.   
  129.   
  130.         'IASK' ,  
  131.   
  132.   
  133.         'SOGOU' ,  
  134.   
  135.   
  136.         'SOGOU'   
  137.   
  138.   
  139.     );  
  140.   
  141.   
  142.   
  143.   
  144.   
  145.     $spider  =  strtolower ( $_SERVER [ 'HTTP_USER_AGENT' ]);  
  146.   
  147.   
  148.   
  149.   
  150.   
  151.     foreach  ( $searchengine_bot  AS  $key  =>  $value )  
  152.   
  153.   
  154.     {  
  155.   
  156.   
  157.         if  ( strpos ( $spider$value ) !== false)  
  158.   
  159.   
  160.         {  
  161.   
  162.   
  163.             $spider  =  $searchengine_name [ $key ];  
  164.   
  165.   
  166.   
  167.   
  168.   
  169.             if  ( $record  === true)  
  170.   
  171.   
  172.             {  
  173.   
  174.   
  175.                   
  176.   
  177.   
  178.                 $GLOBALS [ 'db' ]->autoReplace( $GLOBALS [ 'ecs' ]->table( 'searchengine' ),  array ( 'date'  => local_date( 'Y-m-d' ),  'searchengine'  =>  $spider'count'  => 1),  array ( 'count'  => 1));  
  179.   
  180.   
  181.                   
  182.   
  183.   
  184.                 //蜘蛛爬行记录 author heui   
  185.   
  186.   
  187.                 $record_count  =  $GLOBALS [ 'db' ]->getOne( 'SELECT COUNT(*) FROM '  .  $GLOBALS [ 'ecs' ]->table( 'bot' ));  
  188.   
  189.   
  190.                 if ( $record_count >10000)  
  191.   
  192.   
  193.                 {  
  194.   
  195.   
  196.                     //超过10000开始删除   
  197.   
  198.   
  199.                     $GLOBALS [ 'db' ]->query( "DELETE FROM " . $GLOBALS [ 'ecs' ]->table( 'bot' ). " LIMIT 1" );  
  200.   
  201.   
  202.                 }  
  203.   
  204.   
  205.                   
  206.   
  207.   
  208.                 $server_name   =  $_SERVER [ "SERVER_NAME" ];  
  209.   
  210.   
  211.                 $server_port   =  $_SERVER [ "SERVER_PORT" ];  
  212.   
  213.   
  214.                 $script_name   =  $_SERVER [ "SCRIPT_NAME" ];  
  215.   
  216.   
  217.                 $query_string  =  $_SERVER [ "QUERY_STRING" ];  
  218.   
  219.   
  220.                 $server_ip     =  $_SERVER [ "REMOTE_ADDR" ];  
  221.   
  222.   
  223.   
  224.   
  225.   
  226.                 $url = "http://" . $server_name ;  
  227.   
  228.   
  229.                 if  ( $server_port  !=  "80" )  
  230.   
  231.   
  232.                 {  
  233.   
  234.   
  235.                     $url  =  $url . ":" . $server_port ;  
  236.   
  237.   
  238.                 }                            
  239.   
  240.   
  241.                    $url = $url . $script_name ;  
  242.   
  243.                   if  ( $query_string  != "" )  
  244.   
  245.                 {  
  246.   
  247.   
  248.                     $url = $url . "?" . $query_string ;  
  249.   
  250.   
  251.                 }  
  252.   
  253.   
  254.                   
  255.   
  256.   
  257.                 $sql = "INSERT INTO "  .  $GLOBALS [ 'ecs' ]->table( 'bot' ) .  "("  .  
  258.   
  259.   
  260.                                 "id, url, bot, time, ip) " .  
  261.   
  262.   
  263.                             "VALUES('', '"  .  $url  .  "', '" . $spider . "','" .gmtime(). "','" . $server_ip"')" ;  
  264.   
  265.   
  266.   
  267.   
  268.   
  269.                 $GLOBALS [ 'db' ]->query( $sql );  
  270.   
  271.   
  272.        
  273.   
  274.   
  275.             }  
  276.   
  277.   
  278.   
  279.   
  280.   
  281.             return   $spider ;  
  282.   
  283.   
  284.         }  
  285.   
  286.   
  287.     }  
  288.   
  289.   
  290.   
  291.   
  292.   
  293.     $spider  =  '' ;  
  294.   
  295.   
  296.   
  297.   
  298.   
  299.     return   '' ;  
  300.   
  301.   
  302. }  
/**


 * 判断是否为搜索引擎蜘蛛


 *


 * @access  public


 * @return  string


 */


function is_spider($record = true)


{


    static $spider = NULL;





    if ($spider !== NULL)


    {


        return $spider;


    }





    if (empty($_SERVER['HTTP_USER_AGENT']))


    {


        $spider = '';





        return '';


    }





    $searchengine_bot = array(


        'googlebot',


        'mediapartners-google',


        'baiduspider+',


        'msnbot',


        'yodaobot',


        'yahoo! slurp;',


        'yahoo! slurp china;',


        'iaskspider',


        'sogou web spider',


        'sogou push spider'


    );





    $searchengine_name = array(


        'GOOGLE',


        'GOOGLE ADSENSE',


        'BAIDU',


        'MSN',


        'YODAO',


        'YAHOO',


        'Yahoo China',


        'IASK',


        'SOGOU',


        'SOGOU'


    );





    $spider = strtolower($_SERVER['HTTP_USER_AGENT']);





    foreach ($searchengine_bot AS $key => $value)


    {


        if (strpos($spider, $value) !== false)


        {


            $spider = $searchengine_name[$key];





            if ($record === true)


            {


            	


                $GLOBALS['db']->autoReplace($GLOBALS['ecs']->table('searchengine'), array('date' => local_date('Y-m-d'), 'searchengine' => $spider, 'count' => 1), array('count' => 1));


                


                //蜘蛛爬行记录 author heui


                $record_count = $GLOBALS['db']->getOne('SELECT COUNT(*) FROM ' . $GLOBALS['ecs']->table('bot'));


                if($record_count>10000)


                {


                	//超过10000开始删除


                	$GLOBALS['db']->query("DELETE FROM ".$GLOBALS['ecs']->table('bot')." LIMIT 1");


                }


                


                $server_name  = $_SERVER["SERVER_NAME"];


                $server_port  = $_SERVER["SERVER_PORT"];


                $script_name  = $_SERVER["SCRIPT_NAME"];


                $query_string = $_SERVER["QUERY_STRING"];


                $server_ip    = $_SERVER["REMOTE_ADDR"];





                $url="http://".$server_name;


                if ($server_port != "80")


                {


                	$url = $url.":".$server_port;


                }                          


                   $url=$url.$script_name;

                  if ($query_string !="")

                {


                	$url=$url."?".$query_string;


                }


                


                $sql="INSERT INTO " . $GLOBALS['ecs']->table('bot') . "(" .


                                "id, url, bot, time, ip) ".


                            "VALUES('', '" . $url . "', '".$spider."','".gmtime()."','".$server_ip. "')";





                $GLOBALS['db']->query($sql);


	 


            }





            return $spider;


        }


    }





    $spider = '';





    return '';


}


 3.增加查看蜘蛛爬行列表页面bot.php

Php代码
  1. <?php  
  2.   
  3.   
  4. /**  
  5.  
  6.  
  7.  * 蜘蛛爬行记录列表   
  8.  
  9.  
  10.  * bot.php  
  11.  
  12.  
  13.  * ============================================================================  
  14.  
  15.  
  16.  * @author heui  
  17.  
  18.  
  19.  */   
  20.   
  21.   
  22. define('IN_ECS' , true);  
  23.   
  24.   
  25. require (dirname( __FILE__ ) .  '/includes/init.php' );  
  26.   
  27.   
  28.   
  29.   
  30.   
  31. if  ((DEBUG_MODE & 2) != 2)  
  32.   
  33.   
  34. {  
  35.   
  36.   
  37.     $smarty ->caching = true;  
  38.   
  39.   
  40. }  
  41.   
  42.   
  43. /* 初始化分页信息 */   
  44.   
  45.   
  46. $page  = isset( $_REQUEST [ 'page' ])   &&  intval ( $_REQUEST [ 'page' ])  > 0 ?  intval ( $_REQUEST [ 'page' ])  : 1;  
  47.   
  48.   
  49. $size  = 100;  
  50.   
  51.   
  52.   
  53.   
  54.   
  55. $cache_id  = sprintf( '%X' , crc32( $page . '_' . $size ));  
  56.   
  57.   
  58.   
  59.   
  60.   
  61. if  (! $smarty ->is_cached( 'bot.dwt'$cache_id ))  
  62.   
  63.   
  64. {  
  65.   
  66.   
  67.     $smarty ->assign( 'bot_list' ,     get_bot_list( $page$size ));   
  68.   
  69.   
  70.     $record_count  =  $GLOBALS [ 'db' ]->getOne( 'SELECT COUNT(*) FROM '  .  $GLOBALS [ 'ecs' ]->table( 'bot' ));  
  71.   
  72.   
  73.     $page_count  =  $record_count  > 0 ?  intval ( ceil ( $record_count  /  $size )) : 1;     
  74.   
  75.   
  76.     $smarty ->assign( 'pager' ,       get_pager( 'bot.php'array (),  $record_count$page$size ));       
  77.   
  78.   
  79. }  
  80.   
  81.   
  82. $smarty ->display( 'bot.dwt'$cache_id );  
  83.   
  84.   
  85. /**  
  86.  
  87.  
  88.  * 获取蜘蛛爬行记录列表  
  89.  
  90.  
  91.  * @author heui  
  92.  
  93.  
  94.  * @return  array  
  95.  
  96.  
  97.  */   
  98.   
  99.   
  100. function  get_bot_list( $page$size )  
  101.   
  102.   
  103. {  
  104.   
  105.   
  106.     $sql  =  "SELECT id, url, bot, time, ip FROM "  .  $GLOBALS [ 'ecs' ]->table( 'bot' ).  "ORDER BY time DESC" ;  
  107.   
  108.   
  109.     $res  =  $GLOBALS [ 'db' ]->selectLimit( $sql$size , ( $page  - 1) *  $size );  
  110.   
  111.   
  112.       
  113.   
  114.   
  115.     $arr  =  array ();  
  116.   
  117.   
  118.     while  ( $row  =  $GLOBALS [ 'db' ]->fetchRow( $res ))      
  119.   
  120.   
  121.     {  
  122.   
  123.   
  124.         $arr [ $row [ 'id' ]][ 'id' ]    =  $row [ 'id' ];  
  125.   
  126.   
  127.         $arr [ $row [ 'id' ]][ 'url' ]   =  $row [ 'url' ];  
  128.   
  129.   
  130.         $arr [ $row [ 'id' ]][ 'bot' ]   =  $row [ 'bot' ];  
  131.   
  132.   
  133.         $arr [ $row [ 'id' ]][ 'time' ]  =  date ( "Y-m-j H:i:s" , $row [ 'time' ]);  
  134.   
  135.   
  136.         $arr [ $row [ 'id' ]][ 'ip' ]    =  $row [ 'ip' ];  
  137.   
  138.   
  139.     }  
  140.   
  141.   
  142.   
  143.   
  144.   
  145.     return   $arr ;  
  146.   
  147.   
  148. }  
<?php


/**


 * 蜘蛛爬行记录列表 


 * bot.php


 * ============================================================================


 * @author heui


 */


define('IN_ECS', true);


require(dirname(__FILE__) . '/includes/init.php');





if ((DEBUG_MODE & 2) != 2)


{


	$smarty->caching = true;


}


/* 初始化分页信息 */


$page = isset($_REQUEST['page'])   && intval($_REQUEST['page'])  > 0 ? intval($_REQUEST['page'])  : 1;


$size = 100;





$cache_id = sprintf('%X', crc32($page.'_'.$size));





if (!$smarty->is_cached('bot.dwt', $cache_id))


{


	$smarty->assign('bot_list',     get_bot_list($page, $size));	


	$record_count = $GLOBALS['db']->getOne('SELECT COUNT(*) FROM ' . $GLOBALS['ecs']->table('bot'));


	$page_count = $record_count > 0 ? intval(ceil($record_count / $size)) : 1;	


	$smarty->assign('pager',       get_pager('bot.php', array(), $record_count, $page, $size));	   


}


$smarty->display('bot.dwt', $cache_id);


/**


 * 获取蜘蛛爬行记录列表


 * @author heui


 * @return  array


 */


function get_bot_list($page, $size)


{


	$sql = "SELECT id, url, bot, time, ip FROM " . $GLOBALS['ecs']->table('bot'). "ORDER BY time DESC";


	$res = $GLOBALS['db']->selectLimit($sql, $size, ($page - 1) * $size);


	


	$arr = array();


	while ($row = $GLOBALS['db']->fetchRow($res))	


	{


		$arr[$row['id']]['id']    = $row['id'];


		$arr[$row['id']]['url']   = $row['url'];


		$arr[$row['id']]['bot']   = $row['bot'];


		$arr[$row['id']]['time']  = date("Y-m-j H:i:s",$row['time']);


		$arr[$row['id']]['ip']    = $row['ip'];


	}





	return $arr;


}

 4.增加蜘蛛爬行列表的模板 bot.dwt

Html代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >   
  2.   
  3.   
  4. < html   xmlns = "http://www.w3.org/1999/xhtml" >   
  5.   
  6.   
  7. < head >   
  8.   
  9.   
  10. < meta   http-equiv = "Content-Type"   content = "text/html; charset=utf-8"   />   
  11.   
  12.   
  13. < title > 蜘蛛爬行记录 </ title >   
  14.   
  15.   
  16. < style >   
  17.   
  18.   
  19. ul, li{  
  20.   
  21.   
  22. float:left;  
  23.   
  24.   
  25. list-style: none;  
  26.   
  27.   
  28. }  
  29.   
  30.   
  31. ul{  
  32.   
  33.   
  34. width:900px;  
  35.   
  36.   
  37. }  
  38.   
  39.   
  40. </ style >   
  41.   
  42.   
  43. </ head >   
  44.   
  45.   
  46. < body >   
  47.   
  48.   
  49. < ul   style = "background-color:#FF0000;" >   
  50.   
  51.   
  52. < li   style = "width: 50px;" > id </ li >   
  53.   
  54.   
  55. < li   style = "width: 300px;" > url </ li >   
  56.   
  57.   
  58. < li   style = "width: 100px;" > bot </ li >   
  59.   
  60.   
  61. < li   style = "width: 200px;" > time </ li >   
  62.   
  63.   
  64. < li   style = "width: 200px;" > ip </ li >   
  65.   
  66.   
  67. </ ul >   
  68.   
  69.   
  70. <!--{foreach from=$bot_list item=bot}-->   
  71.   
  72.   
  73. < ul >   
  74.   
  75.   
  76. < li   style = "width: 50px;" > {$bot.id} </ li >   
  77.   
  78.   
  79. < li   style = "width: 300px;" > < a   href = "{$bot.url}"   target = "_blank" > {$bot.url|truncate:30} </ a > </ li >   
  80.   
  81.   
  82. < li   style = "width: 100px;" > {$bot.bot} </ li >   
  83.   
  84.   
  85. < li   style = "width: 200px;" > {$bot.time} </ li >   
  86.   
  87.   
  88. < li   style = "width: 200px;" > {$bot.ip} </ li >   
  89.   
  90.   
  91. </ ul >   
  92.   
  93.   
  94. <!--{/foreach}-->   
  95.   
  96.   
  97.   
  98.   
  99.   
  100. <!--翻页 start-->   
  101.   
  102.   
  103. < ul > < li >   
  104.   
  105.   
  106.   < span   style = "margin-right:10px;" > {$lang.pager_1} < b > {$pager.record_count} </ b >  {$lang.pager_2} </ span >   
  107.   
  108.   
  109.   <!-- {if $pager.page_first} --> < a   href = "{$pager.page_first}" > {$lang.page_first} ... </ a > <!-- {/if} -->   
  110.   
  111.   
  112.   <!-- {if $pager.page_prev} --> < a   class = "prev"   href = "{$pager.page_prev}" > {$lang.page_prev} </ a > <!-- {/if} -->   
  113.   
  114.   
  115.   <!-- {if $pager.page_count neq 1} -->   
  116.   
  117.   
  118.     <!--{foreach from=$pager.page_number key=key item=item}-->   
  119.   
  120.   
  121.       <!-- {if $pager.page eq $key} -->   
  122.   
  123.   
  124.       < span > {$key} </ span >   
  125.   
  126.   
  127.       <!-- {else} -->   
  128.   
  129.   
  130.       < a   href = "{$item}" > [{$key}] </ a >   
  131.   
  132.   
  133.       <!-- {/if} -->   
  134.   
  135.   
  136.     <!--{/foreach}-->   
  137.   
  138.   
  139.   <!-- {/if} -->   
  140.   
  141.   
  142.   
  143.   
  144.   
  145.   <!-- {if $pager.page_next} --> < a   href = "{$pager.page_next}" > {$lang.page_next} </ a > <!-- {/if} -->   
  146.   
  147.   
  148.   <!-- {if $pager.page_last} --> < a   href = "{$pager.page_last}" > ...{$lang.page_last} </ a > <!-- {/if} -->   
  149.   
  150.   
  151. </ li > </ ul >   
  152.   
  153.   
  154. <!--翻页 END-->   
  155.   
  156.   
  157. </ body >   
  158.   
  159.   
  160. </ html >  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值