开发者

Retrieve the country code in PHP

开发者 https://www.devze.com 2023-04-01 02:51 出处:网络
I\'m a little l开发者_如何学JAVAost with that. How can I retrieve the ISO country code of the visitors at one php page?

I'm a little l开发者_如何学JAVAost with that.

How can I retrieve the ISO country code of the visitors at one php page?

Thanks advance


You can either do this by Geolocation of the IP or by inspecting the right headers.

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']. It contains comma-separated entries, e.g.: en-GB,en;q=0.8,en-US;q=0.6,nl;q=0.4 (my own)

The HTTP Accept Language parameters seperates it's languages by a comma, it's properties by a semicolon. The q-value is from 0 to 1, with 1 being the highest/most preferred. 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, e.g. 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

Example

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

Gives US

Check out http://www.hostip.info/use.html for more info.


A library i use myself and can recommend, is 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.

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

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' ) );
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号