[经验分享] 《PHP跳转代码》根据爬虫、设备、来源判断,实现php屏蔽/跳转与展示控制

[复制链接]
sheep 发表于 2022-12-24 16:11 | 显示全部楼层 |阅读模式
PHP多场景跳转模板,内置完整蜘蛛 UA 库,支持三种常用访问控制,配置区域配置对应模块即可,自带防循环重定向,可跳转域名或目录。

《爬虫/蜘蛛/设备标识全量识别清单》


本文中的爬虫/蜘蛛/设备标识更新/补充,均会在上述文章内同步,复制代码后注意核对

  1. <?php
  2. // ==============================================
  3. // PHP 跳转控制通用模板
  4. // 功能:爬虫识别 | 设备识别 | 来源识别 | 防循环重定向
  5. // 支持:域名前缀跳转 / 完整域名跳转 / 指定目录跳转
  6. // ==============================================

  7. // ====================== 【配置区】 按需修改即可 ======================
  8. // 1. 通用配置
  9. $redirect_code = 302;          // 重定向状态码:301=永久重定向  302=临时重定向

  10. // 2. 爬虫配置
  11. $spider_redirect = '';         // 爬虫跳转目标,空=放行不跳转;示例:/spider.html 或 https://xxx.com

  12. // 3. 设备跳转配置(留空=该设备不执行跳转)
  13. $pc_redirect_target = '';      // PC端跳转目标;示例:www1.$base_domain 或 /pc/
  14. $tablet_redirect_target = '';  // 平板端(iPad等)跳转目标;示例:www1.$base_domain 或 /pad/
  15. $mobile_redirect_target = '';  // 手机端跳转目标;示例:m.$base_domain 或 /mobile/

  16. // 4. 来源跳转配置
  17. $non_search_redirect = '';     // 非搜索引擎&非AI来源跳转目标,空=不启用;示例:/error.html
  18. $ai_referer_redirect = '';     // AI平台来源单独跳转目标,空=不单独处理

  19. // 5. 防循环重定向规则(匹配该规则则取消跳转,避免死循环)
  20. // 域名前缀跳转填:^www1\. ;目录跳转填:^/mobile/
  21. $loop_guard_pattern = '';
  22. // ====================== 【配置区结束】 ======================


  23. // ---------------------- 1. 基础域名提取 ----------------------
  24. // 自动移除 www. / www1. / m. / wap. 前缀,提取裸域名
  25. $base_domain = $_SERVER['HTTP_HOST'] ?? '';
  26. if (preg_match('/^(www|www1|m|wap)\.(.+)$/i', $base_domain, $matches)) {
  27.     $base_domain = $matches[2];
  28. }


  29. // ---------------------- 2. 设备类型精准识别 ----------------------
  30. // 初始化标记
  31. $is_pc = false;
  32. $is_tablet = false;
  33. $is_mobile = false;
  34. $ua = $_SERVER['HTTP_USER_AGENT'] ?? '';

  35. // 优先级1:识别手机端
  36. if (preg_match('/mobile|iphone|ipod|android.*mobile|windows\s*phone|blackberry|opera\s*mini|ucweb|meego|silk|kindle|playbook|nintendo|playstation|wii/i', $ua)) {
  37.     $is_mobile = true;
  38. }

  39. // 优先级2:识别平板端(iPad/安卓平板,避免误判为PC)
  40. if (!$is_mobile) {
  41.     if (preg_match('/ipad|android.*tablet|tablet/i', $ua)) {
  42.         $is_tablet = true;
  43.     }
  44. }

  45. // 优先级3:剩余识别为PC桌面端
  46. if (!$is_mobile && !$is_tablet) {
  47.     if (preg_match('/windows\s*nt|macintosh|mac\s*os\s*x|x11|cros|linux\s*(x86|amd64|i686)/i', $ua)) {
  48.         $is_pc = true;
  49.     }
  50. }


  51. // ---------------------- 3. 爬虫/蜘蛛全量识别 ----------------------
  52. // 覆盖范围:通用泛匹配 + 全球主流AI蜘蛛 + 中国主流AI蜘蛛 + 全球搜索引擎 + 中国搜索引擎 + 其他平台蜘蛛
  53. $is_spider = false;
  54. $spiderRegex = '/bot|spider|crawl|crawler|index|archive|gptbot|oai-searchbot|chatgpt-user|claudebot|claude-searchbot|claude-user|xai-searchbot|xai-bot|xai-grok|meta-externalagent|meta-externalfetcher|perplexitybot|perplexity-user|huggingfacebot|amazonbot|coherebot|inflectionbot|google-extended|ccbot|doubaobot|yuanbaobot|hunyuanbot|qwenbot|tongyibot|wenxinbot|moonshotbot|deepseekbot|kimibot|zhipubot|youbot|google|bing|yahoo|yandex|duckduckbot|slurp|applebot|baidu|sogou|bytespider|shenma|youdao|360spider|soso|toutiao|semrush|ahrefs|petalbot|exabot|facebot|facebook|twitter|linkedin|telegram|ia_archiver/i';
  55. if (preg_match($spiderRegex, $ua)) {
  56.     $is_spider = true;
  57. }


  58. // ---------------------- 4. 访问来源(Referrer)识别 ----------------------
  59. $is_search_referer = false;
  60. $is_ai_referer = false;
  61. $referrer = $_SERVER['HTTP_REFERER'] ?? '';

  62. // 识别搜索引擎来源(支持多级域名,如google.co.jp、baidu.com.cn)
  63. $searchReg = '/\.(google|bing|yahoo|yandex|duckduckgo|apple|baidu|sogou|shenma|sm|youdao|360|soso|toutiao)(\.[a-z0-9\-]+){0,2}\//i';
  64. if (preg_match($searchReg, $referrer)) {
  65.     $is_search_referer = true;
  66. }

  67. // 识别AI平台来源
  68. $aiReg = '/\.(openai|anthropic|claude|xai|perplexity|huggingface|cohere|inflection|meta|gemini|doubao|wenxin|tongyi|qianwen|hunyuan|moonshot|deepseek)(\.[a-z0-9\-]+){0,2}\//i';
  69. if (preg_match($aiReg, $referrer)) {
  70.     $is_ai_referer = true;
  71. }


  72. // ---------------------- 5. 跳转目标计算(优先级从高到低) ----------------------
  73. $final_target = '';

  74. // 优先级1:爬虫专属逻辑
  75. if ($is_spider) {
  76.     $final_target = $spider_redirect;
  77. }

  78. // 优先级2:AI平台来源单独跳转(非爬虫时生效)
  79. if (!$is_spider && $is_ai_referer) {
  80.     $final_target = $ai_referer_redirect;
  81. }

  82. // 优先级3:非搜索&非AI来源跳转(非爬虫、未触发AI跳转时生效)
  83. if (!$is_spider && !$is_search_referer && !$is_ai_referer) {
  84.     $final_target = $non_search_redirect;
  85. }

  86. // 优先级4:设备维度跳转(非爬虫、未触发来源跳转时生效)
  87. if (!$is_spider && $final_target === '') {
  88.     if ($is_pc) {
  89.         $final_target = $pc_redirect_target;
  90.     }
  91.     if ($is_tablet) {
  92.         $final_target = $tablet_redirect_target;
  93.     }
  94.     if ($is_mobile) {
  95.         $final_target = $mobile_redirect_target;
  96.     }
  97. }

  98. // 处理域名前缀变量,替换 $base_domain 为实际裸域名
  99. if ($final_target !== '' && strpos($final_target, '$base_domain') !== false) {
  100.     $final_target = str_replace('$base_domain', $base_domain, $final_target);
  101. }


  102. // ---------------------- 6. 防循环重定向检测 ----------------------
  103. if ($loop_guard_pattern !== '') {
  104.     // 域名前缀场景防护
  105.     if (preg_match('/' . $loop_guard_pattern . '/i', $_SERVER['HTTP_HOST'])) {
  106.         $final_target = '';
  107.     }
  108.     // 目录跳转场景防护
  109.     if (preg_match('/' . $loop_guard_pattern . '/i', $_SERVER['REQUEST_URI'])) {
  110.         $final_target = '';
  111.     }
  112. }


  113. // ---------------------- 7. 执行跳转 ----------------------
  114. if ($final_target !== '') {
  115.     // 目标包含http/https → 直接跳转到完整域名
  116.     if (preg_match('/^https?:\/\//i', $final_target)) {
  117.         header('Location: ' . $final_target, true, $redirect_code);
  118.         exit;
  119.     }
  120.     // 目标以/开头 → 站内目录跳转
  121.     else if (str_starts_with($final_target, '/')) {
  122.         header('Location: ' . $final_target, true, $redirect_code);
  123.         exit;
  124.     }
  125.     // 其他情况 → 视为域名前缀,拼接完整域名+原请求路径
  126.     else {
  127.         $fullUrl = 'http://' . $final_target . $_SERVER['REQUEST_URI'];
  128.         header('Location: ' . $fullUrl, true, $redirect_code);
  129.         exit;
  130.     }
  131. }
  132. ?>
复制代码


下一页是独立功能版本

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注公众号
Archiver|手机版|小黑屋|社区规范|绵羊优创

相关侵权、举报、投诉及建议等,请发 E-mail:2363400792@qq.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.|京ICP备19037745号-2公安备案京公网安备11011502037529号

在本版发帖
关注公众号
QQ客服返回顶部
来点音乐
优聚封面
歌曲名称
歌手名称
0:00 0:00
顺序播放
歌词加载中...