如何在 PHP 中跟踪用户位置/区域

发布于 2024-09-27 13:52:32 字数 192 浏览 2 评论 0原文

我正在尝试获取用户浏览网站的国家/地区,以便我可以计算出在网站上显示的货币。我尝试使用以下位置提供的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

四叶草在未来唯美盛开 2024-10-04 13:52:32

我用这个:

  $_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
  $ip = $_SESSION['ip'];
  $try1 = "http://ipinfodb.com/ip_query.php?ip=".$ip."&output=xml";
  $try2 = "http://backup.ipinfodb.com/ip_query.php?ip=".$ip."&output=xml";
  $XML = @simplexml_load_file($try1,NULL,TRUE);
  if(!$XML) { $XML = @simplexml_load_file($try2,NULL,TRUE); }
  if(!$XML) { return false; }

  //Retrieve location, set time
  if($XML->City=="") { $loc = "Localhost / Unknown"; }
  else { $loc = $XML->City.", ".$XML->RegionName.", ".$XML->CountryName; }
  $_SESSION['loc'] = $loc;

I use this:

  $_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
  $ip = $_SESSION['ip'];
  $try1 = "http://ipinfodb.com/ip_query.php?ip=".$ip."&output=xml";
  $try2 = "http://backup.ipinfodb.com/ip_query.php?ip=".$ip."&output=xml";
  $XML = @simplexml_load_file($try1,NULL,TRUE);
  if(!$XML) { $XML = @simplexml_load_file($try2,NULL,TRUE); }
  if(!$XML) { return false; }

  //Retrieve location, set time
  if($XML->City=="") { $loc = "Localhost / Unknown"; }
  else { $loc = $XML->City.", ".$XML->RegionName.", ".$XML->CountryName; }
  $_SESSION['loc'] = $loc;
桃扇骨 2024-10-04 13:52:32

试试这些:

http://ip-to-country.webhosting.info/

http://www.ip2location.com/

两者都是 IP 地址到国家/地区的数据库,可让您查找给定 IP 地址的原产国。

但值得注意的是,这些数据库并非 100% 准确。它们是很好的指南,但您会因各种原因得到错误的结果。

  • 许多人使用代理来绕过特定国家/地区的阻止和过滤器。
  • 许多IP范围被分配给地理分布广泛的公司;您只会得到他们所在的国家/地区,而不是实际机器所在的位置(这对于跟踪 AOL 用户来说一直是一个大问题,因为他们显然都居住在弗吉尼亚州)
  • IP 范围的控制有时会在之间转移国家,因此您可能会得到错误的结果(特别是对于较小/连接较差的国家/地区)

保持数据库最新将减轻其中一些问题,但不会完全解决它们(特别是代理问题),因此,您应该始终考虑到您会得到错误结果的事实。

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.

  • Many people use proxying to get around country-specific blocks and filters.
  • Many IP ranges are assigned to companies with large geographic spread; you'll just get the country where they're based, not where the actual machine is (this always used to be a big problem for tracking AOL users, because they were all apparently living in Virginia)
  • Control of IP ranges are sometimes transferred between countries, so you may get false results from that (especially for smaller/less well-connected countries)

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.

恰似旧人归 2024-10-04 13:52:32

您应该使用 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.

难得心□动 2024-10-04 13:52:32

您可以使用地理定位来获取坐标,然后使用一些服务从中获取国家/地区,但地理定位 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 :)

挥剑断情 2024-10-04 13:52:32

试试这些:

$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)

{

echo $child->getName() . ": " . $child . "<br />";

}

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)

{

echo $child->getName() . ": " . $child . "<br />";

}

时光暖心i 2024-10-04 13:52:32

正如前面的人所建议的,有很多方法可以做到这一点。但我建议您查看 https://github.com/ 上提供的 IP2 PHP 库我们开发的 ip2iq/ip2-lib-php

您可以像下面这样使用它:

<?php
    require_once("Ip2.php");

    $ip2 = new \ip2iq\Ip2();
    $country_code = $ip2->country('8.8.8.8');
    //$country_code === 'US'
?>

它不需要任何 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:

<?php
    require_once("Ip2.php");

    $ip2 = new \ip2iq\Ip2();
    $country_code = $ip2->country('8.8.8.8');
    //$country_code === 'US'
?>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文