RobotCheckBehavior.class.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. namespace Behavior;
  12. /**
  13. * 机器人检测
  14. * @author liu21st <liu21st@gmail.com>
  15. */
  16. class RobotCheckBehavior
  17. {
  18. public function run(&$params)
  19. {
  20. // 机器人访问检测
  21. if (C('LIMIT_ROBOT_VISIT', null, true) && self::isRobot()) {
  22. // 禁止机器人访问
  23. exit('Access Denied');
  24. }
  25. }
  26. private static function isRobot()
  27. {
  28. static $_robot = null;
  29. if (is_null($_robot)) {
  30. $spiders = 'Bot|Crawl|Spider|slurp|sohu-search|lycos|robozilla';
  31. $browsers = 'MSIE|Netscape|Opera|Konqueror|Mozilla';
  32. if (preg_match("/($browsers)/", $_SERVER['HTTP_USER_AGENT'])) {
  33. $_robot = false;
  34. } elseif (preg_match("/($spiders)/", $_SERVER['HTTP_USER_AGENT'])) {
  35. $_robot = true;
  36. } else {
  37. $_robot = false;
  38. }
  39. }
  40. return $_robot;
  41. }
  42. }