代码之家  ›  专栏  ›  技术社区  ›  David Smith

如何检测网站访问者的国家(特别是美国或非美国)?

  •  32
  • David Smith  · 技术社区  · 15 年前

    我需要为我们和非美国访问者显示不同的链接到我的网站。这只是为了方便,所以我不想找一个超高的准确度,和安全或欺骗都不是一个问题。

    我知道有地理定位服务和列表,但这似乎太过分了,因为我只需要(粗略地)确定这个人是否在美国。

    我在考虑使用javascript来获取用户的时区,但这似乎只给出了偏移量,因此加拿大、墨西哥和南美的用户与美国的用户具有相同的价值。

    在javascript或php中是否还有其他可用的信息,除了获取IP地址并进行查找之外,还需要确定这一点?

    8 回复  |  直到 6 年前
        1
  •  48
  •   Christian C. Salvadó    15 年前

    有一些免费服务可以让您从客户端进行基于国家和IP的地理本地化。

    我已经用过了 wipmania 免费的JSONP服务,使用起来非常简单:

    <script type="text/javascript">
      // plain JavaScript example
      function jsonpCallback(data) { 
        alert('Latitude: ' + data.latitude + 
              '\nLongitude: ' + data.longitude + 
              '\nCountry: ' + data.address.country); 
      }
    </script>
    <script src="http://api.wipmania.com/jsonp?callback=jsonpCallback"
            type="text/javascript"></script>
    

    或者,如果您使用支持jsonp的框架(如jquery),则可以:

    // jQuery example
    $.getJSON('http://api.wipmania.com/jsonp?callback=?', function (data) { 
      alert('Latitude: ' + data.latitude + 
            '\nLongitude: ' + data.longitude + 
            '\nCountry: ' + data.address.country); 
    });
    

    检查上面运行的代码段 here .

        2
  •  6
  •   Taylor Leese    15 年前

    最好的指示器可能是HTTP接受语言头。在HTTP请求中,它将如下所示:

    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
    

    您应该能够使用以下方法在PHP中检索到:

    <?php
    echo $_SERVER['HTTP_ACCEPT_LANGUAGE'];
    ?>
    
        3
  •  3
  •   Farinha    15 年前

    我会说地理定位是唯一一种远程可靠的方法。但也有一些情况根本无济于事。我经常去那些认为我在法国的网站,因为我公司的主干网就在那里,所有的互联网流量都通过它。

    HTTP接受头不足以确定用户区域设置。它只告诉您用户选择了什么作为他们的语言,这可能与他们在哪里无关。关于这个的更多信息 here .

        4
  •  1
  •   Mike    15 年前

    根据你想区分哪个国家,时区是实现这一目标的一个非常简单的方法——我认为这是非常可靠的,因为大多数人都会把电脑上的时钟设置正确。(当然,使用这种技术有许多国家你无法区分)。

    下面是一个非常简单的例子,说明如何做到这一点:

    http://unmissabletokyo.com/country-detector

        5
  •  1
  •   Rostislav Levkovic    11 年前

    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;
        }
    }
    ?>
    
        6
  •  1
  •   Nijboer IT    11 年前

    罗斯特斯拉夫

    或使用卷曲:

    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);
    }
    
        7
  •  1
  •   Leonardo Ciaccio    8 年前

    我的解决方案,简单而小型,在这个示例中,我从语言测试加拿大地区 fr-CA or en-CA

    if( preg_match( "/^[a-z]{2}\-(ca)/i", $_SERVER[ "HTTP_ACCEPT_LANGUAGE" ] ) ){
    
       $region = "Canada";
    
    }
    
        8
  •  0
  •   sameeuor    10 年前

    只需使用hostip api

    <?php $country_code = file_get_contents("http://api.hostip.info/country.php"); <br/>if($country_code == "US"){ echo "You Are USA"; } <br/>else{ echo "You Are Not USA";} ?>
    

    所有国家代码都在这里。 http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2