我如何跟踪访问者的国家并将其重定向到适当的网站? [英] How do i track the visitor's country and redirect them to appropriate sites?

查看:102
本文介绍了我如何跟踪访问者的国家并将其重定向到适当的网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想跟踪访问者的国家,然后将它们重定向到我的网站的相应子域名,例如Google所做的......



至多我能依靠api的数据,如果我应该使用任何?..

我使用php ..

解决方案

下载并安装 Maxmind的GeoLite国家/地区数据库,其中声称99.5%的准确性。您可以支付升级到付费版本的99.8%的准确性。关于如何从PHP使用数据库有四个选项:


  1. 下载CSV文件并将其导入您选择的SQL数据库。这是最慢的选项,并且有一个丑陋的API。我不会推荐它,但为了完整起见,包含它。

  2. 下载一个可以读取数据库文件的纯PHP类。这提供了一个比SQL选项好的API,但仍然有点慢。有 PHP4 PHP5

  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

#重定向一个国家
RewriteEngine on
RewriteCond%{ENV:GEOIP_COUNTRY_CODE} ^ CA
RewriteRule ^(。*)$ http://www.canada.com$1 [L]

#将多个国家重定向到一个页面
RewriteEngine on
RewriteCond%{ENV:GEOIP_COUNTRY_CODE} ^(CA | US | MX)$
RewriteRule ^(。*)$ http://www.northamerica.com$1 [L]






当然,更好的方法是让其他人去做为你,在DNS级别。快速搜索显示该公司,(我不知道他们是否好或不好)毫无疑问,还有其他人。这就是谷歌的做法。


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..

解决方案

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.

这篇关于我如何跟踪访问者的国家并将其重定向到适当的网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆