How do you detect a website visitor's country (Specifically, US or not)?

前端 未结 8 2018
無奈伤痛
無奈伤痛 2020-12-12 19:11

I need to show different links for US and non-US visitors to my site. This is for convenience only, so I am not looking for a super-high degree of accuracy, and security or

相关标签:
8条回答
  • 2020-12-12 19:59

    Wipmania.com & PHP

    <?php
    $site_name = "www.your-site-name.com";
    
    function getUserCountry() {
        $fp = fsockopen("api.wipmania.com", 80, $errno, $errstr, 5);
        if (!$fp) {
            // API is currently down, return as "Unknown" :(
            return "XX";
        } else {
            $out = "GET /".$_SERVER['REMOTE_ADDR']."?".$site_name." HTTP/1.1\r\n";
            $out .= "Host: api.wipmania.com\r\n";
            $out .= "Typ: php\r\n";
            $out .= "Ver: 1.0\r\n";
            $out .= "Connection: Close\r\n\r\n";
            fwrite($fp, $out);
            while (!feof($fp)) {
                $country = fgets($fp, 3);
            }
            fclose($fp);
            return $country;
        }
    }
    ?>
    
    0 讨论(0)
  • 2020-12-12 19:59

    @rostislav

    or using cURL:

    public function __construct($site_name) {
        // create a new cURL resource
        $ch = curl_init();
    
        // set URL and other appropriate options
        curl_setopt($ch, CURLOPT_POST, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
        curl_setopt($ch, CURLOPT_URL, "http://api.wipmania.com".$_SERVER['REMOTE_ADDR']."?".$site_name);
        curl_setopt($ch, CURLOPT_HEADER, 0);
    
        // grab URL and pass it to the browser
        $response = curl_exec($ch);
        $info = curl_getinfo($ch,CURLINFO_HTTP_CODE);
    
        if (($response === false) || ($info !== 200)) {
            throw new Exception('HTTP Error calling Wipmania API - HTTP Status: ' . $info . ' - cURL Erorr: ' . curl_error($ch));
        } elseif (curl_errno($ch) > 0) {
            throw new Exception('HTTP Error calling Wipmania API - cURL Error: ' . curl_error($ch));
        }
    
        $this->country = $response;
    
        // close cURL resource, and free up system resources
        curl_close($ch);
    }
    
    0 讨论(0)
  • 2020-12-12 20:02

    I would say that geotargetting is the only method that's even remotely reliable. But there are also cases where it doesn't help at all. I keep getting to sites that think I'm in France because my company's backbone is there and all Internet traffic goes through it.

    The HTTP Accept Header is not enough to determine the user locale. It only tells you what the user selected as their language, which may have nothing to do with where they are. More on this here.

    0 讨论(0)
  • 2020-12-12 20:07

    My solution, easy and small, in this example i test Canada region from language fr-CA or en-CA

    if( preg_match( "/^[a-z]{2}\-(ca)/i", $_SERVER[ "HTTP_ACCEPT_LANGUAGE" ] ) ){
    
       $region = "Canada";
    
    }
    
    0 讨论(0)
  • 2020-12-12 20:08

    Depending on which countries you want to distinguish, time zones can be a very easy way to achieve it - and I assume it's quite reliable as most people will have the clocks on their computers set right. (Though of course there are many countries you can't distinguish using this technique).

    Here's a really simple example of how to do it:

    http://unmissabletokyo.com/country-detector

    0 讨论(0)
  • 2020-12-12 20:12

    The best indicator is probably the HTTP Accept-Language header. It will look something like below in the HTTP request:

    GET / HTTP/1.1
    Accept: */*
    Accept-Language: en-us
    User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64; Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; .NET CLR 3.5.30729; MDDC; OfficeLiveConnector.1.4; OfficeLivePatch.0.0; .NET CLR 3.0.30729)
    Accept-Encoding: gzip, deflate
    Host: www.google.com
    Connection: Keep-Alive
    

    You should be able to retrieve this in PHP using the following:

    <?php
    echo $_SERVER['HTTP_ACCEPT_LANGUAGE'];
    ?>
    
    0 讨论(0)
提交回复
热议问题