PHPackages                             pangongzi/ip - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. pangongzi/ip

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

pangongzi/ip
============

find region by ip

2026.02.04(3mo ago)011MITPHPPHP &gt;=7.0

Since Mar 6Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/pangongzi/ip)[ Packagist](https://packagist.org/packages/pangongzi/ip)[ Docs](https://github.com/pangongzi/ip)[ RSS](/packages/pangongzi-ip/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)DependenciesVersions (7)Used By (0)

### 项目描述

[](#项目描述)

### 安装

[](#安装)

使用 Composer 安装：

```
# 安装
composer require pangongzi/ip

# 更新
composer update pangongzi/ip

# 删除
composer remove pangongzi/ip
```

### 完全基于文件的查询

[](#完全基于文件的查询)

```
$dbFile = "ip2region.xdb file path";
try {
    $searcher = XdbSearcher::newWithFileOnly($dbFile);
} catch (Exception $e) {
    printf("failed to create searcher with '%s': %s\n", $dbFile, $e);
    return;
}

$ip = '1.2.3.4';
$sTime = XdbSearcher::now();
$region = $searcher->search($ip);
if ($region === null) {
    // something is wrong
    printf("failed search(%s)\n", $ip);
    return;
}

printf("{region: %s, took: %.5f ms}\n", $region, XdbSearcher::now() - $sTime);

// 备注：并发使用，每个线程或者协程需要创建一个独立的 searcher 对象。
```

### 缓存 `VectorIndex` 索引

[](#缓存-vectorindex-索引)

如果你的 php 母环境支持，可以预先加载 vectorIndex 缓存，然后做成全局变量，每次创建 Searcher 的时候使用全局的 vectorIndex，可以减少一次固定的 IO 操作从而加速查询，减少 io 压力。

```
// 1、从 dbPath 加载 VectorIndex 缓存，把下述的 vIndex 变量缓存到内存里面。
$vIndex = XdbSearcher::loadVectorIndexFromFile($dbPath);
if ($vIndex === null) {
    printf("failed to load vector index from '%s'\n", $dbPath);
    return;
}

// 2、使用全局的 vIndex 创建带 VectorIndex 缓存的查询对象。
try {
    $searcher = XdbSearcher::newWithVectorIndex($dbFile, $vIndex);
} catch (Exception $e) {
    printf("failed to create vectorIndex cached searcher with '%s': %s\n", $dbFile, $e);
    return;
}

// 3、查询
$sTime = XdbSearcher::now();
$region = $searcher->search('1.2.3.4');
if ($region === null) {
    printf("failed search(1.2.3.4)\n");
    return;
}

printf("{region: %s, took: %.5f ms}\n", $region, XdbSearcher::now() - $sTime);

// 备注：并发使用，每个线程或者协程需要创建一个独立的 searcher 对象，但是都共享统一的只读 vectorIndex。
```

### 缓存整个 `xdb` 数据

[](#缓存整个-xdb-数据)

如果你的 PHP 母环境支持，可以预先加载整个 `xdb` 的数据到内存，这样可以实现完全基于内存的查询，类似之前的 memory search 查询。

```
// 1、从 dbPath 加载整个 xdb 到内存。
$cBuff = XdbSearcher::loadContentFromFile($dbPath);
if ($cBuff === null) {
    printf("failed to load content buffer from '%s'\n", $dbPath);
    return;
}

// 2、使用全局的 cBuff 创建带完全基于内存的查询对象。
try {
    $searcher = XdbSearcher::newWithBuffer($cBuff);
} catch (Exception $e) {
    printf("failed to create buffer cached searcher: %s\n", $dbFile, $e);
    return;
}

// 3、查询
$sTime = XdbSearcher::now();
$region = $searcher->search('1.2.3.4');
if ($region === null) {
    printf("failed search(1.2.3.4)\n");
    return;
}

printf("{region: %s, took: %.5f ms}\n", $region, XdbSearcher::now() - $sTime);

// 备注：并发使用，用整个 xdb 缓存创建的 searcher 对象可以安全用于并发。
```

欢迎补充

### 交流

[](#交流)

email | 邮件

wechat | 微信
[![wechat.jpg](https://camo.githubusercontent.com/d08d19059c096c193063e26926ff78c61486d54abb2142fb054efca9cb79f6a9/68747470733a2f2f692e706f7374696d672e63632f68767657325757772f7765636861742e6a7067)](https://postimg.cc/S2BvKPK7)

###  Health Score

35

—

LowBetter than 79% of packages

Maintenance84

Actively maintained with recent releases

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~67 days

Recently: every ~84 days

Total

6

Last Release

100d ago

Major Versions

v2025.03.21 → 2026.02.042026-02-04

### Community

Maintainers

![](https://www.gravatar.com/avatar/d7c6bb02ffa6f42d57b09b9c06b2584dfb6c86389a0db95a4ba68c460bc35ef0?d=identicon)[pangongzi](/maintainers/pangongzi)

---

Top Contributors

[![pangongzi](https://avatars.githubusercontent.com/u/6062750?v=4)](https://github.com/pangongzi "pangongzi (8 commits)")

---

Tags

phpIPlocation

### Embed Badge

![Health badge](/badges/pangongzi-ip/health.svg)

```
[![Health](https://phpackages.com/badges/pangongzi-ip/health.svg)](https://phpackages.com/packages/pangongzi-ip)
```

###  Alternatives

[stevebauman/location

Retrieve a user's location by their IP Address

1.3k7.6M65](/packages/stevebauman-location)[zhuzhichao/ip-location-zh

get the ip's location info with Chinese

617212.5k11](/packages/zhuzhichao-ip-location-zh)[gai871013/ip-location

纯真IP地址,composer包(解析QQWay.dat)

729.7k4](/packages/gai871013-ip-location)[kartik-v/yii2-ipinfo

An IP address information display widget for Yii 2.0 with country flag and geo position info.

2832.6k1](/packages/kartik-v-yii2-ipinfo)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
