<?php/*** IP2Region 防火墙使用示例*/// 记录开始时间$startTime = microtime(true);// 方法 1: 使用中间件(推荐)require_once __DIR__ . '/Middleware.php';$middleware = new FirewallMiddleware();if (!$middleware->handle()) {// 计算被拒绝时的耗时并输出(可选,因为 handle 中已 exit,但为了逻辑完整保留此处注释说明)// 实际上 showBlockedPage 中已经 exit,所以这里不会执行到exit;}$ip = $middleware->getClientIP();echo "<pre />欢迎访问!你的 IP{$ip} 位置信息正常。\n";$ipdata = $middleware->firewall->getIPLocation($ip);echo "位置:" . $ipdata['country'] . ' ' . $ipdata['province'] . ' ' . $ipdata['city'] . "\n";$ip = '58.221.171.22';$result = $middleware->firewall->getIPLocation($ip);echo "位置:" . $result['country'] . ' ' . $result['province'] . ' ' . $result['city'] . "\n";// 计算方法 1 耗时$endTime = microtime(true);$duration = ($endTime - $startTime) * 1000;echo "【性能统计】方法 1 执行耗时:" . number_format($duration, 4) . " ms\n";// ============================================// 方法 2: 直接使用防火墙类/*require_once __DIR__ . '/mod/ip2region/Firewall.php';// 重新记录方法 2 的开始时间(如果取消注释独立运行)$startTime2 = microtime(true);$firewall = new Ip2RegionFirewall();$clientIP = $_SERVER['REMOTE_ADDR'];$result = $firewall->checkAccess($clientIP);if (!$result['allow']) {echo "访问被拒绝:" . $result['reason'] . "\n";$endTime2 = microtime(true);$duration2 = ($endTime2 - $startTime2) * 1000;echo "【性能统计】方法 2 (拒绝) 执行耗时:" . number_format($duration2, 4) . " ms\n";exit;}echo "欢迎访问!\n";echo "您的 IP: " . $clientIP . "\n";echo "位置:" . $result['location']['country'] . ' ' . $result['location']['province'] . ' ' . $result['location']['city'] . "\n";// 计算方法 2 耗时$endTime2 = microtime(true);$duration2 = ($endTime2 - $startTime2) * 1000;echo "【性能统计】方法 2 执行耗时:" . number_format($duration2, 4) . " ms\n";*/
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。