定制 arraypress/visitor-country 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

arraypress/visitor-country

最新稳定版本:1.0.0

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.

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0-or-later
  • 更新时间: 2026-05-06

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固