RobotCheckBehavior.class.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. defined('THINK_PATH') or exit();
  12. /**
  13. * 机器人检测
  14. * @category Extend
  15. * @package Extend
  16. * @subpackage Behavior
  17. * @author liu21st <liu21st@gmail.com>
  18. */
  19. class RobotCheckBehavior extends Behavior {
  20. protected $options = array(
  21. 'LIMIT_ROBOT_VISIT' => true, // 禁止机器人访问
  22. );
  23. public function run(&$params) {
  24. // 机器人访问检测
  25. if(C('LIMIT_ROBOT_VISIT') && self::isRobot()) {
  26. // 禁止机器人访问
  27. exit('Access Denied');
  28. }
  29. }
  30. static private function isRobot() {
  31. static $_robot = null;
  32. if(is_null($_robot)) {
  33. $spiders = 'Bot|Crawl|Spider|slurp|sohu-search|lycos|robozilla';
  34. $browsers = 'MSIE|Netscape|Opera|Konqueror|Mozilla';
  35. if(preg_match("/($browsers)/", $_SERVER['HTTP_USER_AGENT'])) {
  36. $_robot = false ;
  37. } elseif(preg_match("/($spiders)/", $_SERVER['HTTP_USER_AGENT'])) {
  38. $_robot = true;
  39. } else {
  40. $_robot = false;
  41. }
  42. }
  43. return $_robot;
  44. }
  45. }