>  기사  >  백엔드 개발  >  웹사이트를 방문하는 검색 엔진 스파이더의 발자국을 기록하는 PHP 방법_php 팁

웹사이트를 방문하는 검색 엔진 스파이더의 발자국을 기록하는 PHP 방법_php 팁

WBOY
WBOY원래의
2016-05-16 20:17:331127검색

이 기사의 예에서는 웹사이트를 방문하는 검색 엔진 스파이더의 발자국을 PHP가 기록하는 방법을 설명합니다. 참고할 수 있도록 모든 사람과 공유하세요. 구체적인 분석은 다음과 같습니다.

검색 엔진 스파이더는 페이지를 원격으로 크롤링하여 웹사이트에 접속합니다. JS 코드를 사용하여 스파이더의 에이전트 정보를 얻을 수는 없지만, 이미지 태그를 사용하면 에이전트 데이터를 분석하여 얻을 수 있습니다. , 우리는 거미의 유형, 성별 및 기타 요소를 결정할 수 있으며 이를 데이터베이스나 텍스트에 기록하여 통계를 만들 수 있습니다.

데이터베이스 구조:

인용된 내용은 다음과 같습니다.

#
# 表的结构 `naps_stats_bot`
#

CREATE TABLE `naps_stats_bot` (
`botid` int(10) unsigned NOT NULL auto_increment,
`botname` varchar(100) NOT NULL default '',
`botagent` varchar(200) NOT NULL default '',
`bottag` varchar(100) NOT NULL default '',
`botcount` int(11) NOT NULL default '0',
`botlast` datetime NOT NULL default '0000-00-00 00:00:00',
`botlasturl` varchar(250) NOT NULL default '',
UNIQUE KEY `botid` (`botid`),
KEY `botname` (`botname`)
) TYPE=MyISAM AUTO_INCREMENT=9 ;
#
# 导出表中的数据 `naps_stats_bot`
#
INSERT INTO `naps_stats_bot` VALUES (1, 'Googlebot', 'Googlebot/2.X (+http://www.googlebot.com/bot.html)', 'googlebot', 0, '0000-00-00 00:00:00', '');
INSERT INTO `naps_stats_bot` VALUES (2, 'MSNbot', 'MSNBOT/0.1 (http://search.msn.com/msnbot.htm)', 'msnbot', 0, '0000-00-00 00:00:00', '');
INSERT INTO `naps_stats_bot` VALUES (3, 'Inktomi Slurp', 'Slurp/2.0', 'slurp', 0, '0000-00-00 00:00:00', '');
INSERT INTO `naps_stats_bot` VALUES (4, 'Baiduspider', 'Baiduspider+(+http://www.baidu.com/search/spider.htm)', 'baiduspider', 0, '0000-00-00 00:00:00', '');
INSERT INTO `naps_stats_bot` VALUES (5, 'Yahoobot', 'Mozilla/5.0+(compatible;+Yahoo!+Slurp;+http://help.yahoo.com/help/us/ysearch/slurp)', 'slurp', 0, '0000-00-00 00:00:00', '');
INSERT INTO `naps_stats_bot` VALUES (6, 'Sohubot', 'sohu-search', 'sohu-search', 0, '0000-00-00 00:00:00', '');
INSERT INTO `naps_stats_bot` VALUES (7, 'Lycos', 'Lycos/x.x', 'lycos', 0, '0000-00-00 00:00:00', '');
INSERT INTO `naps_stats_bot` VALUES (8, 'Robozilla', 'Robozilla/1.0', 'robozilla', 0, '0000-00-00 00:00:00', '');

PHP 프로그램은 다음과 같습니다.

인용된 내용은 다음과 같습니다.

<&#63;php
/************************
* NAPS -- Network Article Publish System
* ----------------------------------------------
*     bot.php
*     -------------------
*  begin  : 2004-08-15
*
************************/
/************************
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License.
*
************************/
/************************
*
*  NAPS产品是自由软件。你可以且必须根据《GNU GPL-GNU通用公共许可证》的相关规定
*  复制、修改及分发NAPS产品。任何以NAPS产品为基础的衍生发行版未必须经过飘飘的授权。
*
************************/
error_reporting(E_ALL & ~E_NOTICE);
function get_naps_bot()
{
 $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
 if (strpos($useragent, 'googlebot') !== false){
  return 'Googlebot';
 }
 if (strpos($useragent, 'msnbot') !== false){
  return 'MSNbot';
 }
 if (strpos($useragent, 'slurp') !== false){
  return 'Yahoobot';
 }
 if (strpos($useragent, 'baiduspider') !== false){
  return 'Baiduspider';
 }
 if (strpos($useragent, 'sohu-search') !== false){
  return 'Sohubot';
 }
 if (strpos($useragent, 'lycos') !== false){
  return 'Lycos';
 }
 if (strpos($useragent, 'robozilla') !== false){
  return 'Robozilla';
 }    
 return false;
}
$tlc_thispage = addslashes($_SERVER['HTTP_USER_AGENT']);
//添加蜘蛛的抓取记录
$searchbot = get_naps_bot();
if ($searchbot) {
 $DB_naps->query("UPDATE naps_stats_bot SET botcount=botcount+1, botlast=NOW(), botlasturl='$tlc_thispage' WHERE botname='$searchbot'");
}
&#63;>

이 기사가 모든 사람의 PHP 프로그래밍 설계에 도움이 되기를 바랍니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.