javascript 获取站点访问者位置的最佳方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8556785/
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
What is the best way to get a site visitor's location?
提问by B Seven
After googling, I found many solutions that don't seem to work or require permission from the user.
谷歌搜索后,我发现许多解决方案似乎不起作用或需要用户许可。
Looking for something that is easy to use, PHP or Javascript, and does not require the user's permission. It doesn't need to be the most accurate.
寻找易于使用的东西,PHP 或 Javascript,并且不需要用户许可。它不需要是最准确的。
回答by Vigrond
Google Analytics is great for this (javascript).
谷歌分析非常适合这个(javascript)。
EDIT: Should point out that Google Analytics has an API to get City/Country info and all reporting info if you need to programmatically do something with it - http://code.google.com/apis/analytics/docs/
编辑:应该指出 Google Analytics 有一个 API 来获取城市/国家信息和所有报告信息,如果您需要以编程方式对其进行处理 - http://code.google.com/apis/analytics/docs/
But if that's not an option:
但如果这不是一个选择:
You can get a users ip with PHP's $_SERVER['REMOTE_ADDR']
您可以使用 PHP 获取用户 ip $_SERVER['REMOTE_ADDR']
With that, you can use PHP's GeoIP library to geolocate it: http://php.net/manual/en/book.geoip.php
有了它,您可以使用 PHP 的 GeoIP 库来对其进行地理定位:http: //php.net/manual/en/book.geoip.php
回答by check123
This is what I use (you can get IP by $_SERVER['REMOTE_ADDR']
and pass it to the function):
这是我使用的(您可以通过获取 IP$_SERVER['REMOTE_ADDR']
并将其传递给函数):
function GeoData($ip)
{
$Content = CurlGet("http://api.hostip.info/get_html.php?ip=" . $ip . "&position=true");
$ContentArr = explode("\n", $Content);
$Stack = array();
$Ctr = 0;
foreach($ContentArr as $Item)
{
if($Ctr == 2)
{
$Ctr++;
continue;
}
if($Ctr == 6)
break;
$SingleItemArr = explode(":", $Item);
array_push($Stack, $SingleItemArr[1]);
$Ctr++;
}
$MappedStr = array("country" => $Stack[0], "city" => $Stack[1], "latitude" => $Stack[2], "longitude" => $Stack[3], "ip" => $Stack[4]);
$JsonEncoded = json_encode($MappedStr);
return $JsonEncoded;
}
回答by Mad Man Moon
I think it's very important to point out that querying the GEO IP database from MaxMind via php is an excellent solution, if the site is going to have any amount of traffic beyond that of a personal site you are going to have to invest in a MaxMind license. Having had experience integrating MaxMind for high volume e-commerce retail sites, the cost can be quite significant (although the benefits in regards to payment systems is well worth it).
我认为需要指出的是,通过 php 从 MaxMind 查询 GEO IP 数据库是一个很好的解决方案,如果该站点的流量超出个人站点的流量,您将不得不投资 MaxMind执照。拥有将 MaxMind 集成到大量电子商务零售网站的经验,成本可能相当高(尽管支付系统方面的好处是值得的)。
One more comment. GeoIP is only accurate to the general coverage area of the ISP's local DNS server that the user is connected to. It is not GPS. It generally does not get down to the street level. And, if the user is connecting through a proxy, VPN, etc, the data/info is useless.
再发表一点意见。GeoIP 仅准确到用户连接到的 ISP 本地 DNS 服务器的一般覆盖区域。它不是 GPS。它通常不会下降到街道水平。而且,如果用户通过代理、VPN 等连接,数据/信息将毫无用处。