arraypress/visitor-country
Composer 安装命令:
composer require arraypress/visitor-country
包简介
Resolve a visitor's country from CDN and server-level request headers (Cloudflare, AWS CloudFront, Fastly, BunnyCDN, mod_geoip). Zero dependencies, framework-agnostic, signature-gated to defeat trivial spoofing.
README 文档
README
Resolve a visitor's country (ISO-3166 alpha-2) from CDN and server-level request headers.
Zero dependencies, framework-agnostic, signature-gated to defeat trivial spoofing. Bundles sources for Cloudflare, AWS CloudFront, Fastly, BunnyCDN, server-level GeoIP modules (Apache mod_geoip / nginx GeoIP), and a generic X-Country-Code fallback. Register your own sources for custom GeoIP databases.
Why
Most fraud-detection / analytics / geo-routing code reaches for a paid IP-lookup API to answer "where is this visitor from?" — but if your site sits behind Cloudflare, CloudFront, Fastly, or any other major CDN, the answer's already in the request headers, for free. This library reads those headers in the right order with the right safety checks, and returns the country code.
Install
composer require arraypress/visitor-country
Usage
use ArrayPress\VisitorCountry\Country; // Simple — returns "GB" or empty string on miss $country = Country::resolve(); // With provenance — useful for logging / debugging $result = Country::resolve_detailed(); $result->get_country(); // 'GB' $result->get_source(); // 'cloudflare' $result->get_confidence(); // 'high' $result->has_country(); // true $result->to_array(); // [ 'country' => 'GB', 'source' => 'cloudflare', 'confidence' => 'high' ] // Pass a custom server array (testability) Country::resolve( [ 'HTTP_CF_IPCOUNTRY' => 'GB', 'HTTP_CF_RAY' => 'abc' ] );
Sources
Tried in this order by default. First hit wins.
| Source | Header | Confidence | Sig-gated? |
|---|---|---|---|
| Cloudflare | HTTP_CF_IPCOUNTRY |
high | yes (CF-Ray / CF-Visitor / CF-Connecting-IP) |
| AWS CloudFront | HTTP_CLOUDFRONT_VIEWER_COUNTRY |
high | no |
| Fastly | HTTP_FASTLY_CLIENT_COUNTRY |
high | yes (Fastly-FF / Fastly-Client-IP) |
| BunnyCDN | HTTP_CDN_LOOPCOUNTRY |
high | no |
| Server GeoIP | GEOIP_COUNTRY_CODE (Apache mod_geoip / nginx ngx_http_geoip_module) |
medium | no |
| Generic | HTTP_X_COUNTRY_CODE |
low | no |
Signature gating — for headers that an attacker hitting the origin directly could trivially fake (Cloudflare, Fastly), the resolver verifies that a vendor-set companion header is also present before trusting the country. Country-spoofing alone is rarely a critical attack surface, but cheap to defend against.
Sentinel filtering — Cloudflare returns XX for unknown geo and T1 for Tor exits; both are filtered out so a downstream source can take over.
Shape validation — only ISO-3166 alpha-2 (2 letters, A-Z) values are returned.
Custom sources
Got a local MaxMind GeoLite2 database? Register a callback:
use ArrayPress\VisitorCountry\Country; use ArrayPress\VisitorCountry\Sources\Callback; Country::add_source( new Callback( fn( $server ) => MyGeoIP::lookup( $server['REMOTE_ADDR'] ?? '' ), 'maxmind_local', 'high' ) );
add_source() appends to the chain (lowest priority). Use prepend_source() to take precedence over the bundled CDN sources.
To replace the chain entirely:
Country::set_sources( [ new Cloudflare(), new MyCustomSource() ] );
To reset to the default chain (useful in tests):
Country::reset();
Implementing your own Source
use ArrayPress\VisitorCountry\Source; class MyMaxMindSource implements Source { public function get_name(): string { return 'maxmind'; } public function get_confidence(): string { return 'high'; } public function resolve( array $server ): ?string { $ip = $server['REMOTE_ADDR'] ?? ''; if ( $ip === '' ) { return null; } // ... lookup logic ... return $country_code; // or null } }
If your source reads from a single header with optional signature gating, extend Sources\HeaderSource and just declare the header name + signatures + sentinels.
Requirements
- PHP 7.4+
- No other dependencies
License
GPL-2.0-or-later.
arraypress/visitor-country 适用场景与选型建议
arraypress/visitor-country 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 05 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「geolocation」 「geoip」 「cloudfront」 「cloudflare」 「country」 「fastly」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 arraypress/visitor-country 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 arraypress/visitor-country 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 arraypress/visitor-country 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A GeoIP decorator/processor for monolog
Maps in minutes. Powered by the Google Maps API.
CloudFront cache purger for the Blitz plugin.
Amazon Web Services (AWS) Toolbox - This extension connects your TYPO3 instance to Amazon CloudFront. It rewrites all file paths in the frontend to match your CDN domain. You also have the possibility to invalidate Amazon CloudFront entries.
The official PHP library for apiip.net that allowing customers to automate IP address validation and geolocation lookup in websites, applications and back-office system. Visit our API docs at https://apiip.net/documentation
CDN Cache Control and Invalidation
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 52
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-or-later
- 更新时间: 2026-05-06