php 检测用户国家的最快方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5206015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fastest Way Of Detecting User's Country
提问by Eray
I need detect user's country and show website's language by him / her country . (Turkish for Turkish people, English for all others)
我需要检测用户的国家并按他/她的国家显示网站的语言。(土耳其语为土耳其人,英语为所有其他人)
How can i do this fastest way ? Performance is important for me .
我怎样才能以最快的方式做到这一点?性能对我来说很重要。
I'm looking IPInfoDB' API, are there any better alternative ?
我正在寻找IPInfoDB 的 API,有没有更好的选择?
(I'm using PHP)
(我正在使用 PHP)
回答by chiefo
Well for people who might visit in 2017 this is a solution this extremely simple to use
对于可能在 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>
let me beak it down letter A - H is the script to detect your ISO number for the country for my country Nigeria it is NG you can google search your countries ISO number, with this script it is auto detected. Then i created a page with some info you fire AJAX to that page which it sends back the country flag color and name simple and easy PLEASE CALL JQUERY BEFORE AJAX TO RUN THE AJAX UNLESS IT WONT WORK GOODLUCK
让我把它写下来 字母 A - H 是检测您所在国家/地区的 ISO 编号的脚本 尼日利亚 它是 NG 您可以谷歌搜索您所在国家/地区的 ISO 编号,使用此脚本可以自动检测到它。然后我创建了一个页面,其中包含一些信息,您将 AJAX 发送到该页面,该页面将国旗颜色和名称发回简单易行请在 AJAX 运行 AJAX 之前调用 JQUERY,除非它不会工作 好运
回答by cabaret
You could use the API here http://www.hostip.info/use.htmlif you're okay with relying on an external site.
如果您可以依赖外部站点,您可以在http://www.hostip.info/use.html使用 API 。
You can also use the GeoIP PHP API
您还可以使用GeoIP PHP API
Of course, implementing the script Orbit linked might just save you the hassle of going through the API's.
当然,实现脚本轨道链接可能只是为您省去浏览 API 的麻烦。
Good luck.
祝你好运。
回答by joan16v
The best way to do this I have found is using the "GAE-IP-TO-COUNTRY" library: https://github.com/andris9/GAE-IP-TO-COUNTRY/tree/master/src/ip_files
我发现最好的方法是使用“GAE-IP-TO-COUNTRY”库:https: //github.com/andris9/GAE-IP-TO-COUNTRY/tree/master/src/ip_files
Example of use (you have to copy the "ip_files" directory to your server):
使用示例(您必须将“ip_files”目录复制到您的服务器):
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;
回答by middus
As others have pointed out, it would probably a better idea to check the Accept-Language
HTTP Header for Turkish. If it's the preferred language, serve it. Otherwise serve English.
Here's some code.
正如其他人指出的那样,检查Accept-Language
土耳其语的HTTP 标头可能是一个更好的主意。如果它是首选语言,请提供它。否则服务英语。这是一些代码。
回答by hamboy75
I coded the next stuff using Accept-Language as other users pointed:
正如其他用户指出的那样,我使用 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;
}
A simple
一个简单的
print_r(GetAcceptedLangs());
return in my case:
在我的情况下返回:
Array ( [0] => es-ES [1] => es [2] => en )
You can after define an array like this one to change to your internal language value, for example:
您可以在定义这样的数组后更改为您的内部语言值,例如:
$al={"ES-es"=>"es","es"=>"es","en"=>"en"......}
They are already sorted by user preferences.
它们已经按用户偏好排序。
If all languages don't exists in the array you can go to the default language of your website. This is valid also if the browser don't send the Accept-Language header.
如果数组中不存在所有语言,您可以转到您网站的默认语言。如果浏览器不发送 Accept-Language 标头,这也有效。
Another version removing the sub-region values
另一个删除子区域值的版本
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;
}
It would return in my case
在我的情况下它会返回
Array ( [0] => es [1] => en )
回答by Nejat Joseph Mhango
Here is the straight forward way I like to use
这是我喜欢使用的直接方式
<?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;
?>
Visit this website for more info
访问此网站了解更多信息