简体   繁体   English

检索PHP中的国家/地区代码

[英]Retrieve the country code in PHP

I'm a little lost with that. 我对此有些迷茫。

How can I retrieve the ISO country code of the visitors at one php page? 如何在一个php页面上检索访客的ISO国家代码?

Thanks advance 谢谢前进

You can either do this by Geolocation of the IP or by inspecting the right headers. 您可以通过IP的地理位置定位,也可以通过检查正确的标题来实现。

Usually you want the latter, since it tells you which languages the browser/system uses. 通常,您需要后者,因为它会告诉您浏览器/系统使用哪种语言。 You will only want to use geolocation when you want to know the physical location. 当您想知道实际位置时,才需要使用地理位置。 The header is stored in $_SERVER['HTTP_ACCEPT_LANGUAGE'] . 标头存储在$_SERVER['HTTP_ACCEPT_LANGUAGE'] It contains comma-separated entries, eg: en-GB,en;q=0.8,en-US;q=0.6,nl;q=0.4 (my own) 它包含逗号分隔的条目,例如: en-GB,en;q=0.8,en-US;q=0.6,nl;q=0.4 (我自己)

The HTTP Accept Language parameters seperates it's languages by a comma, it's properties by a semicolon. HTTP接受语言参数用逗号分隔语言,属性用分号分隔。 The q-value is from 0 to 1, with 1 being the highest/most preferred. q值从0到1,其中1为最高/最优选。 Here is some naive and untested code to parse it: 这是一些幼稚且未经测试的代码来解析它:

$langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
$preffered = "";
$prefvalue = 0;
foreach($langs as $lang){
    $info = explode(';', $lang);
    $val = (isset($lang[1])?$lang[1];1);
    if($prefvalue < $val){
        $preferred = $lang[0];
        $prefvalue = $val;
    }
}

Much simpler is it if you want to test if a specific language is accepted, eg Spanish (es): 如果要测试是否接受特定语言(例如西班牙语),则要简单得多:

if(strpos($_SERVER['HTTP_ACCEPT_LANGUAGE'], "es") !== false){
    // Spanish is supported
}

I think you could use this php script which uses an ip and prints out a country code 我认为您可以使用此php脚本,该脚本使用ip并输出国家/地区代码

Example

http://api.hostip.info/country.php?ip=4.2.2.2

Gives US US

Check out http://www.hostip.info/use.html for more info. 请访问http://www.hostip.info/use.html了解更多信息。

found this, might be usefull. 发现这一点,可能有用。 Click http://ipinfodb.com/ip_location_api.php ! 点击http://ipinfodb.com/ip_location_api.php

A library i use myself and can recommend, is MaxMind GeoLite Country . 我自己使用并可以推荐的图书馆是MaxMind GeoLite Country To get the country code, you need only to copy 2 files to your server, the php code geoip.inc and the binary data GeoIP.dat . 要获取国家/地区代码,您只需要将2个文件复制到服务器上,即php代码geoip.inc和二进制数据GeoIP.dat

Using the library is also very straightforward: 使用该库也非常简单:

function ipToCountry()
{
  include_once('geoip/geoip.inc');

  $gi = geoip_open(__DIR__ . '/geoip/GeoIP.dat', GEOIP_STANDARD);
  $result = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
  geoip_close($gi);

  return $result;
}

This will use GeoIp and fall back to accept_lang 这将使用GeoIp并回退到accept_lang

class Ip2Country
{
    function get( $target )
    {
        $country = false;
        if( function_exists( 'geoip_record_by_name' ) )
            $country = $this->getFromIp( $target );
        if( !$country && isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) )
            $country = $this->getFromLang( $_SERVER['HTTP_ACCEPT_LANGUAGE'] );
        return $country;
    }

    function getFromIp( $target )
    {
        $dat = @geoip_record_by_name( $target );
        return ( isset( $dat['country_code'] ) ) ? mb_strtolower( $dat['country_code'] ) : false;
    }

    function getFromLang( $str )
    {
        $info = array();
        $langs = explode( ',', $str );
        foreach( $langs as $lang )
        {
            $i = explode( ';', $lang );
            $j = array();
            if( !isset( $i[0] ) ) continue;
            $j['code'] = $i[0];
            if( strstr( $j['code'], '-' ) )
            {
                $parts = explode( '-', $j['code'] );
                $j['lang'] = $parts[0];
                $j['country'] = mb_strtolower( $parts[1] );
            }
            $info[] = $j;
        }
        return ( isset( $info[0]['country'] ) ) ? $info[0]['country'] : false;
    }
}

$x = new Ip2Country();
var_dump( $x->get( 'canada.ca' ) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM