How do i track the visitor's country and redirect them to appropriate sites?

前端 未结 5 426
深忆病人
深忆病人 2021-02-04 14:16

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 d

相关标签:
5条回答
  • 2021-02-04 14:23

    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.

    0 讨论(0)
  • 2021-02-04 14:23

    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

    0 讨论(0)
  • 2021-02-04 14:36

    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.

    0 讨论(0)
  • 2021-02-04 14:40

    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.

    0 讨论(0)
  • 2021-02-04 14:40

    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");
    
    0 讨论(0)
提交回复
热议问题