旧版本的typecho的access插件记录的ip太过于落后了。判断也有很多问题,所以需要在此更新一下。

定位到地址文件是lib下面的ipipfree.ipdb,删掉这个就好,用不到了。这个最近的时间是2019年的。真的很无语的。

接下来定位到所在位置是Access_Core.php这个文件的这里代码。(Access_IpDb.php这个也可以删除)

            try {
                $ipdb = new Access_IpDb(dirname(__file__).'/lib/ipipfree.ipdb');
                $city = $ipdb->findInfo($ip, 'CN');
                 // 写入日志
                error_log("IP: {$ip}\nCity Info: " . print_r($city, true), 3, '/tmp/access_debug.log');
                $ip_country = $city->country_name;
                if($ip_country == '中国') {
                    $ip_province = $city->region_name;
                    $ip_city = $city->city_name;
                } else {
                    $ip_province = $ip_city = NULL;
                }
            } catch(Exception $e) {
                $ip_country = $ip_province = $ip_city = '未知';
            }
            
            

然后根据数据格式,改成他的数据样式的。

try {
                // 检查必要的文件是否存在
                $dbFile = dirname(__FILE__) . '/lib/ip2region.xdb';
                $classFile = dirname(__FILE__) . '/lib/XdbSearcher.php';
                
                if (!file_exists($dbFile)) {
                    throw new Exception("Ip2region database file not found: {$dbFile}");
                }
                
                if (!file_exists($classFile)) {
                    throw new Exception("XdbSearcher class file not found: {$classFile}");
                }
                
                require_once $classFile;
                
                $searcher = XdbSearcher::newWithFileOnly($dbFile);
                $region = $searcher->search($ip);
                
                if ($region === null) {
                    throw new Exception("IP2Region search failed for IP: {$ip}");
                }
                
                // 调试日志
                error_log("IP: {$ip}\nRegion Info: " . print_r($region, true), 3, '/tmp/access_debug.log');
                
                // 解析数据 (格式: 国家|区域|省份|城市|ISP)
                $regionArray = explode('|', $region);
                
                // 清理数据
                $cleanData = function($data) {
                    return (!empty($data) && $data !== '0') ? $data : '';
                };
                
                $country = $cleanData($regionArray[0]);
                $region_info = $cleanData($regionArray[1]);
                $province = $cleanData($regionArray[2]);
                $city = $cleanData($regionArray[3]);
                
                // 设置最终保存到数据库的字段
                $ip_country = !empty($country) ? $country : '未知';
                
                // 无论国内外都保存区域和城市信息
                $ip_province = '';
                $ip_city = '';
                
                if (!empty($province)) {
                    $ip_province = $province;
                } elseif (!empty($region_info)) {
                    // 如果省份为空,但区域不为空,可以用区域代替
                    $ip_province = $region_info;
                }
                
                if (!empty($city)) {
                    $ip_city = $city;
                }
                
            } catch(Exception $e) {
                error_log("IP解析异常:" . $e->getMessage(), 3, '/tmp/access_debug.log');
                $ip_country = '未知';
                $ip_province = '';
                $ip_city = '';
            }

这样就可以了。

最后修改:2025 年 10 月 14 日
如果觉得我的文章对你有用,请随意赞赏