Use a PHP program to check whether a spider accesses your website (with code)

Source: Internet
Author: User
The search engine crawlers can 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 so that we can

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:

  1. #
  2. # Table structure 'Naps _ stats_bot'
  3. #
  4.  
  5. Create table 'Naps _ stats_bot '(
  6. 'Botid' int (10) unsigned not null auto_increment,
  7. 'Botname' varchar (100) not null default '',
  8. 'Botagent' varchar (200) not null default '',
  9. 'Bottag' varchar (100) not null default '',
  10. 'Botcount' int (11) not null default '0 ',
  11. 'Botlast 'datetime not null default '2017-00-00 00:00:00 ',
  12. 'Botlasturl' varchar (250) not null default '',
  13. Unique key 'botid' ('botid '),
  14. KEY 'botname' ('botname ')
  15. ) TYPE = MyISAM AUTO_INCREMENT = 9;
  16.  
  17. #
  18. # Export the table data 'Naps _ stats_bot'
  19. #
  20.  
  21. Insert into 'Naps _ stats_bot 'VALUES (1, 'googlebot', 'googlebot/2.X (http://www.googlebot.com/bot.html) ', 'googlebot', 0, '2017-00-00 00:00:00 ', '');
  22. Insert into 'Naps _ stats_bot 'VALUES (2, 'msnbot', 'msnbot/0.1 (http://search.msn.com/msnbot.htm) ', 'msnbot', 0, '2017-00-00 00:00:00 ', '');
  23. Insert into 'Naps _ stats_bot 'VALUES (3, 'inktomi slurp', 'slurp/123456', 'slurp', 0, '2017-00-00 00:00:00 ', '');
  24. Insert into 'Naps _ stats_bot 'VALUES (4, 'baidider Ider', 'baidider Ider (http://www.baidu.com/search/spider.htm) ', 'baidider Ider', 0, '2017-00-00 00:00:00 ', '');
  25. 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 ','');
  26. Insert into 'Naps _ stats_bot 'VALUES (6, 'sohubot', 'sohu-search', 'sohu-search', 0, '2017-00-00 00:00:00 ', '');
  27. Insert into 'Naps _ stats_bot 'VALUES (7, 'lycos', 'lycos/x. x', 'lycos ', 0, '2017-00-00 00:00:00 ','');
  28. Insert into 'Naps _ stats_bot 'VALUES (8, 'robozilla', 'robozilla/123456', 'robozilla ', 0, '2017-00-00 00:00:00 ','');

PHP program:

  1. Error_reporting (E_ALL &~ E_NOTICE );
  2.  
  3. Function get_naps_bot ()
  4. {
  5. $ Useragent = strtolower ($ _ SERVER ['http _ USER_AGENT ']);
  6.  
  7. If (strpos ($ useragent, 'googlebot ')! = False ){
  8. Return 'bot bot ';
  9. }
  10.  
  11. If (strpos ($ useragent, 'msnbot ')! = False ){
  12. Return 'msnbot ';
  13. }
  14.  
  15. If (strpos ($ useragent, 'slurp ')! = False ){
  16. Return 'yahoobot ';
  17. }
  18.  
  19. If (strpos ($ useragent, 'baidider Ider ')! = False ){
  20. Return 'baidider Ider ';
  21. }
  22.  
  23. If (strpos ($ useragent, 'sohu-search ')! = False ){
  24. Return 'sohubot ';
  25. }
  26.  
  27. If (strpos ($ useragent, 'lycos ')! = False ){
  28. Return 'lycos ';
  29. }
  30.  
  31. If (strpos ($ useragent, 'robozilla ')! = False ){
  32. Return 'robozilla ';
  33. }
  34. Return false;
  35. }
  36. $ Tlc_thispage = addslashes ($ _ SERVER ['http _ USER_AGENT ']);
  37. // Add a crawler record
  38. $ Searchbot = get_naps_bot ();
  39. If ($ searchbot ){
  40. $ DB_naps-> query ("UPDATE naps_stats_bot SET botcount = botcount 1, botlast = NOW (), botlasturl = '$ tlc_thispage' WHERE botname = '$ searchbot '");
  41. }
  42. ?>

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.