Use PHP to determine whether a search engine spider accesses your website

Source: Internet
Author: User

The search engine crawlers access websites by capturing pages remotely. We cannot use JS Code to obtain the agent information of the spider, but we can use the Image Tag, in this way, we can obtain the agent data of the Spider. By analyzing the agent data, we can determine the types, gender, and other factors of the spider, we can record statistics through databases or text.
Database Structure:

#
# Table structure '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 & prime ;,
'Botlast 'datetime not null default' 0000-00-00 00:00:00 & prime ;,
'Botlasturl' varchar (250) not null default '',
Unique key 'botid' ('botid '),
Key 'botname' ('botname ')
) Type = MyISAM auto_increment = 9;

#
# Export the table data 'naps _ stats_bot'
#

Insert into 'naps _ stats_bot 'values (1, 'googlebot', 'googlebot/2.x (+ http://www.googlebot.com/bot.html) ', 'googlebot', 0, '2014-00-00 00:00:00 & prime ;,'');
Insert into 'naps _ stats_bot 'values (2, 'msnbot', 'msnbot/0.1 (http://search.msn.com/msnbot.htm) ', 'msnbot', 0, '2014-00-00 00:00:00 & prime ;,'');
Insert into 'naps _ stats_bot 'values (3, 'inktomi slup', 'slupp/2.0 & prime;, 'slup', 0, '2014-00-00 00:00:00 & prime ;,'');
Insert into 'naps _ stats_bot 'values (4, 'baidider ider', 'baidider ider + (+ http://www.baidu.com/search/spider.htm) ', 'baidider ider', 0, '2014-00-00 00:00:00 & prime ;,'');
Insert into 'naps _ stats_bot 'values (5, 'yahoobot', 'mozilla/5.0 + (compatible; + Yahoo! + Slurp; + http://help.yahoo.com/help/us/ysearch/slurp) ', 'slurp', 0, '2017-00-00 00:00:00 & prime ;,'');
Insert into 'naps _ stats_bot 'values (6, 'sohubot', 'sohu-search', 'sohu-search', 0, '2017-00-00 00:00:00 & prime ;, '');
Insert into 'naps _ stats_bot 'values (7, 'lycos', 'lycos/X. x', 'lycos ', 0, '2017-00-00 00:00:00 & prime ;,'');
Insert into 'naps _ stats_bot 'values (8, 'robozilla', 'robozilla/1.0 & prime;, 'robozilla ', 0, '2017-00-00 00:00:00 & prime ;, '');

PHP program:

<? PHP


Error_reporting (e_all &~ E_notice );

Function get_naps_bot ()
{
$ Useragent = strtolower ($ _ server ['HTTP _ user_agent ']);

If (strpos ($ useragent, 'googlebot ')! = False ){
Return 'bot bot ';
}

If (strpos ($ useragent, 'msnbot ')! = False ){
Return 'msnbot ';
}

If (strpos ($ useragent, 'slurp ')! = False ){
Return 'yahoobot ';
}

If (strpos ($ useragent, 'baidider ider ')! = False ){
Return 'baidider ider ';
}

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 ']);
// Add a crawler record
$ 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 '");
}

?>

# PHP Column

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.