如何跟踪访问者所在的国家/地区并将其重定向到适当的网站?

发布于 2024-09-01 08:44:09 字数 112 浏览 2 评论 0 原文

我想跟踪访问者的国家/地区,然后将他们重定向到我网站的适当子域,就像谷歌所做的那样...

如果我应该使用任何API,我可以在多大程度上依赖API的数据?...

我是使用PHP..

I want to track the country of the visitors and then redirect them to appropriate subdomains of my site like what google does...

And upto what extent i can rely on the data of the api if i should use any?..

I'm using php..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

三生池水覆流年 2024-09-08 08:44:09

下载并安装 Maxmind 的 GeoLite Country 数据库,该数据库声称准确度为 99.5%。您可以付费升级到付费版本,据称准确率高达 99.8%。关于如何从 PHP 使用数据库,有四个选项:

  1. 下载 CSV 文件并将其导入到您选择的 SQL 数据库中。这是最慢的选项,并且 API 很丑陋。我不会推荐它,但为了完整性起见将其包含在内。
  2. 下载一个可以读取数据库文件的纯PHP类。这提供了比 SQL 选项更好的 API,但仍然有点慢。有 PHP4PHP5
  3. 下载并安装 PHP 的 C 扩展模块。这样做的优点是拥有漂亮的界面和良好的性能。然而,它比纯 PHP 类更难安装,因此您可能需要咨询您的托管公司。如果幸运的话,它可能已经安装了。
  4. 如果您需要极快的性能,请使用 Apache 模块。它是最难安装的,但也是最快的选择。

所有这些选项都有一个获取国家/地区代码的方法。我不会包含 SQL 选项,但您可以此处获取更多信息

纯 PHP4:

include("geoip.inc");
$gi = geoip_open("/usr/local/share/GeoIP/GeoIP.dat",GEOIP_STANDARD);
$code = geoip_country_code_by_addr($gi, "24.24.24.24");

PECL PHP 扩展:

$code = geoip_country_code_by_name($_SERVER['REMOTE_ADDR']);

Apache 模块:

$code = apache_note("GEOIP_COUNTRY_CODE");

然后您可以使用 HTTP 重定向基于这些代码进行重定向:

 $redirect = array("AU" => "http://australia.example.com", "NZ" => "http://newzealand.example.com")
 $url = $redirect[$code]
 header("Location: $url");

但这会导致两个 HTTP 请求,因此不是最佳选择。更好的方法是使用 Apache 模块在您的 .htaccess 文件中进行重写:

GeoIPEnable On
GeoIPDBFile /path/to/GeoIP.dat

# Redirect one country
RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CA
RewriteRule ^(.*)$ http://www.canada.com$1 [L]

# Redirect multiple countries to a single page
RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(CA|US|MX)$
RewriteRule ^(.*)$ http://www.northamerica.com$1 [L]

当然,可以说更好的方法是让其他人在 DNS 处为您执行此操作等级。快速搜索发现这家公司,(我不知道他们是否好)毫无疑问还有其他公司。谷歌就是这样做的。

Download and install Maxmind's GeoLite Country database, which claims 99.5% accuracy. You can pay to upgrade to a paid version with a claimed 99.8% accuracy. There are four options with respect to how to use the database from PHP:

  1. Download the CSV file and import it into your SQL database of choice. This is the slowest option, and has an ugly API. I wouldn't recommend it, but its included for sake of completeness.
  2. Download a pure PHP class which can read the database file. This provides a nice API than the SQL option, but is still a little slow. There are versions for PHP4 and PHP5
  3. Download and install the C extension module for PHP. This has the advantage of having a nice interface and good performance. It is more difficult to install than the pure PHP class however, so you might need to check with your hosting company. If you're lucky, it may already be installed.
  4. If you need blazingly fast performance, go with the Apache module. It is the most difficult to install, but also the fastest option.

All these options has a method for getting a country code. I won't include SQL option, but you can get more info here.

Pure PHP4:

include("geoip.inc");
$gi = geoip_open("/usr/local/share/GeoIP/GeoIP.dat",GEOIP_STANDARD);
$code = geoip_country_code_by_addr($gi, "24.24.24.24");

PECL PHP extension:

$code = geoip_country_code_by_name($_SERVER['REMOTE_ADDR']);

Apache module:

$code = apache_note("GEOIP_COUNTRY_CODE");

You can then redirect based on these codes, using an HTTP redirect:

 $redirect = array("AU" => "http://australia.example.com", "NZ" => "http://newzealand.example.com")
 $url = $redirect[$code]
 header("Location: $url");

This causes two HTTP requests however, and is thus suboptimal. A better approach is to use the Apache module to do rewriting, in your .htaccess file:

GeoIPEnable On
GeoIPDBFile /path/to/GeoIP.dat

# Redirect one country
RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CA
RewriteRule ^(.*)$ http://www.canada.com$1 [L]

# Redirect multiple countries to a single page
RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(CA|US|MX)$
RewriteRule ^(.*)$ http://www.northamerica.com$1 [L]

Of course, an arguably better way to do this is to get someone else to do it for you, at the DNS level. A quick search revealed this company, (I have no idea if they are good or not) no doubt there are others. This is how Google does it.

静待花开 2024-09-08 08:44:09

根据您想要划分客户端“世界”的粒度以及可能的目标(子域)的数量,您可以使用 IP 到国家/地区数据库MaxMind.com 的免费地理位置数据库,将其放入 SQL 表中,然后使用一个小脚本对访问者的 IP 地址执行查找并根据需要进行重定向。

数据永远不会 100% 准确,特别是如果您的任何访问者通过代理或其他匿名程序访问您的网站(在这种情况下,您看到的 IP 地址不是他们的真实地址)。如果您确实实现了这一点,我建议允许用户纠正(或简单地覆盖)区域重定向将是值得包括的。

Dependent on the granularity to which you want to divide your client "world" and the number of possible targets (subdomains) you could use the data provided in the IP-to-country database or MaxMind.com's Free Geolocation Database, place it into an SQL table and then use a small script to perform a lookup on the visitor's IP address and redirect as required.

The data will never be 100% accurate, especially if any of your visitors access your site via proxies or other anonymisers (in which case the IP address you see is not their true address). If you do implement this, I would suggest that allowing the user to correct (or simply override) the regional redirection would be worth including.

怪我鬧 2024-09-08 08:44:09

如果您不想自己编写代码,Evan 有一个很酷的脚本,名为 Lambda GeoIP!在这里查看:http://www.lambdageoip.com

Lambda GeoIP 是一个完全自托管的地理定位您可以在网站上使用该脚本来确定访问者的地理位置。除此之外,您还可以确定用户是否使用移动设备、机器人或使用卫星连接。

您还可以根据用户所在的洲、国家、地区或城市将他们转发到不同的页面。

只需 1 行 PHP 代码即可在您的网站上使用所有功能。

Lambda GeoIP 不会“打电话回家”、加载远程内容或与任何其他服务器通信。它是安全的并且完全自托管。

If you don't feel like coding it yourself, Evan has a cool script called Lambda GeoIP! Check it out here: http://www.lambdageoip.com

Lambda GeoIP is a fully self-hosted geo-targeting script that you can use on your website to determine the geographic location of your visitors. In addition to that, you can determine if a user is on a mobile device, a bot, or using a satellite connection.

You can also forward users to different pages based on their continent, country, region, or city.

All of the features can be used on your site with just 1 line of code in PHP.

Lambda GeoIP does not "phone home", load remote content, or communicate with any other server. It is secure and fully self hosted.

终难遇 2024-09-08 08:44:09

如果您不介意在服务器上下载、安装并保持最新的 IP 数据库,那么 Fmark 的解决方案在性能方面是最好的。
否则,您可以使用重定向服务,例如 www.redirectbycountry.com

If you don't mind downloading, installing and keeping up-to-date an ip database on your server then the solution of Fmark is the best in terms of performances.
Otherwise you can use a redirection service like www.redirectbycountry.com

温柔女人霸气范 2024-09-08 08:44:09

一种版本是您可以使用服务提供商的 IP 地址,然后使用 Javascript 对其进行重定向,如下所示;

$.get("https://ipinfo.io", function(response) {
   if(response.country=="GB"){
      window.location.replace("http://stackoverflow.com");
   }
}, "jsonp");

One version is that you can use your service providers' ip address and hence redirect it with Javascript as follows;

$.get("https://ipinfo.io", function(response) {
   if(response.country=="GB"){
      window.location.replace("http://stackoverflow.com");
   }
}, "jsonp");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文