如何在 PHP 中跟踪用户位置/区域
我正在尝试获取用户浏览网站的国家/地区,以便我可以计算出在网站上显示的货币。我尝试使用以下位置提供的 GET 脚本: http://api.hostip.info 但它们只返回 XX我测试一下。
如果有人知道更好的方法请分享。
谢谢。
I'm trying to get the country from which the user is browsing the website so I can work out what currency to show on the website. I have tried using the GET scripts available from: http://api.hostip.info but they just return XX when I test it.
If anyone knows any better methods please share.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我用这个:
I use this:
试试这些:
http://ip-to-country.webhosting.info/
http://www.ip2location.com/
两者都是 IP 地址到国家/地区的数据库,可让您查找给定 IP 地址的原产国。
但值得注意的是,这些数据库并非 100% 准确。它们是很好的指南,但您会因各种原因得到错误的结果。
保持数据库最新将减轻其中一些问题,但不会完全解决它们(特别是代理问题),因此,您应该始终考虑到您会得到错误结果的事实。
Try these:
http://ip-to-country.webhosting.info/
http://www.ip2location.com/
Both are IP address-to-country databases, which allow you to look up the country of origin of a given IP address.
However it's important to note that these databases are not 100% accurate. They're a good guide, but you will get false results for a variety of reasons.
Keeping your database up-to-date will mitigate some of these issues, but won't resolve them entirely (especially the proxying issue), so you should always allow for the fact that you will get false results.
您应该使用 geoip 库。
Maxmind提供免费数据库和商业数据库,最后更新日期和精度有所不同,商业的质量更好。
请参阅 http://www.maxmind.com/app/geolitecountry 了解免费数据库。
我认为应该足以满足基本需求。
You should use the geoip library.
Maxmind provides free databases and commercial databases, with a difference in the date of last update and precision, the commercial being of better quality.
See http://www.maxmind.com/app/geolitecountry for the free database.
I think it should be sufficient for basic needs.
您可以使用地理定位来获取坐标,然后使用一些服务从中获取国家/地区,但地理定位 API 是基于浏览器的,因此您只能通过 JavaScript 访问它,然后必须以某种方式将这些信息传递给 PHP,我在JS 部分一次:
http://www.lautr.com /utilizing-html5-geolocation-api-and-yahoo-placefinder-example
当谈到通过 IP 获取位置时,有无数的服务提供数据库,有些是免费的,有些是收费的,有些存储了大量 IP 和大量数据,有些存储较少,例如您提到的那个,工作得很好:
http://api.hostip.info/?ip=192.0.32.10
所以你可以使用 Geolocation API,它非常简洁,但需要用户许可,通过 JS 工作,但不在 IE 中工作(到目前为止)或者必须寻找适合您需求的 IPÜ 定位服务:)
You can use Geolocation to get the Coordinates and then some Service to get the Country from that, but the geolocation API is browser based so you can only access it via JavaScript and then have to pass theese Informations to PHP somehow, i wrote something on the JS Part once:
http://www.lautr.com/utilizing-html5-geolocation-api-and-yahoo-placefinder-example
When it comes to getting the Location via the IP, there are a bazillion Services out there who offer databases for that, some free, some for charge, some with a lot of IP's stored and much data, some with less, for example the one you mentioned, works just fine:
http://api.hostip.info/?ip=192.0.32.10
So You can ether go with the Geolocation API which is pretty neat, but requires the users permission, works via JS and doesnt work in IE (so far) or have to look for a IPÜ Location Service that fits your needs :)
试试这些:
$key="9dcde915a1a065fbaf14165f00fcc0461b8d0a6b43889614e8acdb8343e2cf15";
$ip=“198.168.1230.122”;
$url = "http://api.ipinfodb.com/v3/ip-city /?key=$key&ip=$ip&format=xml";
// 加载 xml 文件
$xml = simplexml_load_file($url);
// 打印第一个元素的名称
echo $xml->getName() . “
”;
// 创建一个循环来打印每个节点的元素名称和数据
foreach($xml->children() as $child)
{
}
Try these:
$key="9dcde915a1a065fbaf14165f00fcc0461b8d0a6b43889614e8acdb8343e2cf15";
$ip= "198.168.1230.122";
$url = "http://api.ipinfodb.com/v3/ip-city/?key=$key&ip=$ip&format=xml";
// load xml file
$xml = simplexml_load_file($url);
// print the name of the first element
echo $xml->getName() . "
";
// create a loop to print the element name and data for each node
foreach($xml->children() as $child)
{
}
正如前面的人所建议的,有很多方法可以做到这一点。但我建议您查看 https://github.com/ 上提供的 IP2 PHP 库我们开发的 ip2iq/ip2-lib-php。
您可以像下面这样使用它:
它不需要任何 SQL 或 Web 服务查找,只需要本地数据文件。它比几乎所有其他方法都快。该数据库每月更新一次,您可以免费下载。
如果您需要您语言中的国家/地区名称,您现在需要做的唯一一件事是将其映射到类似这样的关联数组 https://gist.github.com/DHS/1340150
There are many ways to do it as suggested by those earlier. But I suggest you take a look at the IP2 PHP library available at https://github.com/ip2iq/ip2-lib-php which we developed.
You can use it like below:
It doesn't need any SQL or web service lookup just a local data file. It is faster than almost all other methods out there. The database is updated monthly you can download it for free.
The only thing you will need to do for now if you need the country name in your language is map it to an associative array from something like this https://gist.github.com/DHS/1340150