检测用户所在国家的最快方式

时间:2011-03-05 18:50:17

标签: php detection

我需要检测用户的国家/地区并显示他/她所在国家/地区的网站语言。 (土耳其语为土耳其语,英语为所有其他人)

我怎样才能以最快的方式做到这一点?性能对我来说很重要。

我正在寻找IPInfoDB' API,还有更好的选择吗?

(我正在使用PHP)

7 个答案:

答案 0 :(得分:4)

对于2017年可能会访问的人来说,这是一个非常简单易用的解决方案

<button class="btn dropdown-toggle" style="cursor:pointer;z-index:1000!important;margin-top:-67px;background:none!important;font-size:1.4em;" onclick="window.location.href='language'">
(a)  <?php
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
$url = "http://api.wipmania.com/".$ip;
(h) $country = file_get_contents($url); //ISO code for Nigeria it is NG check your country ISO code
?>

<?php if ($country == ""){
echo "Country not found";   
} else { ?>
<script>
var map = "<?php echo $country ?>";
$.ajax({
type : 'POST', 
url : 'http://www.mowemy.com/countryflag.php',
data :  {map: map}, 
success : function(r) { 
//console.log("success: " + r); 
$('#mumil').html(r); 
} })
</script>
<?php } ?>

<div id ="mumil" style="font-size:13px!important;margin-top:5px;"></div>
</button>

让我把它放在字母A - H是检测你的国家的ISO编号的脚本我的国家尼日利亚它是NG你可以谷歌搜索你的国家ISO号码,用这个脚本自动检测。然后我创建了一个页面,其中包含一些信息,你将AJAX发送到该页面,它发送回国家国旗颜色和名称简单易用请在AJAX之前发出通知,以便运行AJAX,除非它工作好运

答案 1 :(得分:3)

如果您依赖外部网站,可以在http://www.hostip.info/use.html使用API​​。

您还可以使用GeoIP PHP API

当然,实现链接的Orbit链接可能只是为您节省了通过API的麻烦。

祝你好运。

答案 2 :(得分:2)

我发现最好的方法是使用&#34; GAE-IP-TO-COUNTRY&#34;图书馆: https://github.com/andris9/GAE-IP-TO-COUNTRY/tree/master/src/ip_files

使用示例(您必须将&#34; ip_files&#34;目录复制到您的服务器):

function iptocountry($ip) {    
    $numbers = preg_split( "/\./", $ip);    
    include("./ip_files/".$numbers[0].".php");
    $code=($numbers[0] * 16777216) + ($numbers[1] * 65536) + ($numbers[2] * 256) + ($numbers[3]);    
    foreach($ranges as $key => $value){
        if($key<=$code){
            if($ranges[$key][0]>=$code){$country=$ranges[$key][1];break;}
        }
    }
    return $country;
}

$country=iptocountry($_SERVER['REMOTE_ADDR']);
echo $country;

答案 3 :(得分:1)

此链接应该非常有用:

http://www.phptutorial.info/iptocountry/the_script.html

您可以从那里下载必要的文件和脚本。

答案 4 :(得分:1)

正如其他人所指出的,检查土耳其语Accept-Language HTTP标头可能更好。如果它是首选语言,请提供服务。否则服务英语。 这是some code

答案 5 :(得分:0)

我使用Accept-Language编写了下一个东西,正如其他用户指出的那样:

function GetAcceptedLangs()
{
    $res=array();
    $a=getallheaders();
    if(isset($a["Accept-Language"]))
    {
        $aceptlangs=explode(",",str_replace(array(';','0','1','2','3','4','5','6','7','8','9','.',"q="),array(',','','','','','','','','','','','',""),$a["Accept-Language"]));
        foreach($aceptlangs as $i=>$v)
        {
            if(trim($v)!="")
                $res[]=trim($v);
        }
    }
    return $res;
}

一个简单的

print_r(GetAcceptedLangs());

返回我的案例:

Array ( [0] => es-ES [1] => es [2] => en )

您可以在定义像这样的数组后更改为您的内部语言值,例如:

$al={"ES-es"=>"es","es"=>"es","en"=>"en"......}

它们已按用户首选项排序。

如果阵列中不存在所有语言,您可以使用网站的默认语言。如果浏览器不发送Accept-Language标头,这也是有效的。

删除子区域值的另一个版本

function GetAcceptedLangs2()
{
    $res=array();
    $a=getallheaders();
    if(isset($a["Accept-Language"]))
    {
        $aceptlangs=explode(",",str_replace(array(';','0','1','2','3','4','5','6','7','8','9','.',"q="),array(',','','','','','','','','','','','',""),$a["Accept-Language"]));
        foreach($aceptlangs as $i=>$v)
        {
            $va=trim($v);
            if(($pos=strpos($va,"-"))!==false)
                $l=substr($va,0,$pos);
            else
                $l=$va;
            if($l!="" && !isset($check[$l]))
            {
                $check[$l]=1;
                $res[]=$l;
            }
        }
    }
    return $res;
}

它会在我的情况下返回

Array ( [0] => es [1] => en )

答案 6 :(得分:0)

这是我喜欢使用的直接方式

<?php
class GeoIp
{
    protected $api = 'https://ipapi.co/%s/json/';

    protected $properties = [];

    //------------------------------------------------------
    /**
    *   __get
    *
    * @access public
    * - @param $key string
    * - @return string / bool
    */  
    public function __get($key)
    {
        if( isset($this->properties[$key]))
        {
            return $this->properties[$key];
        }

        return null;
    }

    //------------------------------------------------------
    /**
    *   request_geoip
    *
    * @access public
    * - @return void
    */
    public function request_geoip($ip)
    {
        $url = sprintf($this->api, $ip);
        $data = $this->send_geoip_request($url);
        $this->properties =  json_decode($data, true);
        //var_dump($this->properties);
    }

    //------------------------------------------------------
    /**
    *   send_geoip_request
    *
    * @access public
    * - @param $url string
    * - @return json
    */
    public function send_geoip_request($url)
    {
        $loc_content = file_get_contents($url); 

        return $loc_content;
    }
}

//You can access it like this:
$geo = new GeoIp;
$geo->request_geoip('foo');
echo $geo->country;

?>

访问this website 了解详情

相关问题