Pbootcms屏蔽指定地区IP访问
Pbootcms屏蔽指定地区IP访问,代码如下;
<?php /** * @copyright (C)2016-2099 Hnaoyun Inc. * @author XingMeng * @emAIl hnxsh@foxmail.com * @date 2016年11月5日 * 用户前端入口文件 */ // ========== IP地理位置限制检查 ========== // 获取真实客户端IP地址 function getRealClientIP() { $ip = ''; if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); $ip = trim($ips[0]); } elseif (isset($_SERVER['HTTP_X_REAL_IP']) && !empty($_SERVER['HTTP_X_REAL_IP'])) { $ip = $_SERVER['HTTP_X_REAL_IP']; } elseif (isset($_SERVER['HTTP_CLIENT_IP']) && !empty($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif (isset($_SERVER['REMOTE_ADDR']) && !empty($_SERVER['REMOTE_ADDR'])) { $ip = $_SERVER['REMOTE_ADDR']; } // 验证IP格式 if (!filter_var($ip, FILTER_VALIDATE_IP)) { $ip = $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0'; } return $ip; } // 搜索引擎核心标识(精简版) $search_engines = [ // Google 'Googlebot', // Bing 'Bingbot', 'MSNBot', // Yahoo 'Slurp', // DuckDuckGo 'DuckDuckBot', // 百度 'Baiduspider', // Yandex 'YandexBot', // 360搜索 '360Spider', // 神马搜索 'YisouSpider', // 头条搜索 'Bytespider', ]; // 获取User-Agent $user_agent = $_SERVER['HTTP_USER_AGENT'] ?? ''; // 检查是否为搜索引擎 $is_search_engine = false; if (!empty($user_agent)) { foreach ($search_engines as $engine) { if (stripos($user_agent, $engine) !== false) { $is_search_engine = true; break; } } } // 白名单IP(用于测试或管理,可以添加您的IP) $whitelist_ips = [ // '127.0.0.1', // 本地测试 '101.30.42.207', // 添加您的IP到这里 ]; // 获取客户端IP $client_ip = getRealClientIP(); // 定义允许的国家代码列表(根据您的需求修改) $allowed_countries = ['PE', 'AR']; // 秘鲁和阿根廷 // 检查是否在白名单中 if (!in_array($client_ip, $whitelist_ips) && !$is_search_engine) { // 如果不是搜索引擎,检查地理位置 // 使用免费API检查IP地理位置 $GEO_api_url = "http://ip-api.com/json/{$client_ip}?fields=status,countryCode,message"; // 设置超时时间(3秒) $context = stream_context_create([ 'http' => [ 'timeout' => 3, 'user_agent' => 'Mozilla/5.0' ] ]); $geo_data = @file_get_contents($geo_api_url, false, $context); if ($geo_data) { $geo_info = json_decode($geo_data, true); // 检查API返回状态和国家代码 if (isset($geo_info['status']) && $geo_info['status'] === 'success') { $country_code = $geo_info['countryCode'] ?? ''; // 如果不在允许的国家列表中,拒绝访问 if (!empty($country_code) && !in_array($country_code, $allowed_countries)) { http_response_code(403); header('Content-Type: text/html; charset=utf-8'); die('<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1"> <title>访问被拒绝</title> <style> * { box-sizing: border-box; margin: 0; padding: 0; } body { font-family: Arial, "Microsoft YaHei", sans-serif; background: #f5f5f5; min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; } .error-box { width: 100%; max-width: 480px; background: #fff; padding: 32px 24px; border-radius: 12px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); text-align: center; } h1 { color: #d32f2f; margin-bottom: 16px; font-size: 22px; font-weight: 600; } p { color: #666; line-height: 1.7; font-size: 14px; margin-bottom: 6px; } @media (min-width: 768px) { .error-box { padding: 40px 32px; } h1 { font-size: 26px; } p { font-size: 15px; } } </style> </head> <body> <div> <h1>访问被拒绝</h1> <p>抱歉,此网站仅限指定地区访问。</p> <p>Sorry, this website is only accessible in designated regions.</p> </div> </body> </html>'); } } } // 如果API请求失败,可以选择允许访问(避免API故障导致网站无法访问) // 或者拒绝访问(更安全),根据需求选择: // else { // http_response_code(403); // die('无法验证地理位置,访问被拒绝。'); // } } // ========== IP限制检查结束 ========== // 定义为入口文件 define('IS_INDEX', true); // 入口文件地址绑定 define('URL_BIND', 'home'); // PHP版本检测 if (version_compare(phpversion(),'7.0.0','<')) { header('Content-Type:text/html; charset=utf-8'); exit('您服务器PHP的版本太低,程序要求PHP版本不小于7.0'); } // 引用内核启动文件 require dirname(__FILE__) . '/core/start.php';
