如何检测网站访问者所在的国家/地区(具体来说,美国与否)?

发布于 2024-08-20 00:14:31 字数 262 浏览 12 评论 0原文

我需要为我的网站的美国和非美国访问者显示不同的链接。这只是为了方便,所以我并不是在寻求超高的准确性,安全或欺骗也不是问题。

我知道有地理定位服务和列表,但这似乎有点矫枉过正,因为我只需要(大致)确定该人是否在美国。

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

除了获取 IP 地址并进行查找之外,JavaScript 或 PHP 中是否还有其他可用信息来确定这一点?

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 spoofing are not a concern.

I know there are geotargeting services and lists, but this seems like overkill since I only need to determine (roughly) if the person is in the US or not.

I was thinking about using JavaScript to get the user's timezone, but this appears to only give the offset, so users in Canada, Mexico, and South America would have the same value as people in the US.

Are there any other bits of information available either in JavaScript, or PHP, short of grabbing the IP address and doing a lookup, to determine this?

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

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

发布评论

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

评论(8

回忆追雨的时光 2024-08-27 00:14:31

有一些免费服务可让您从客户端进行基于国家/地区和 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); 
});

此处检查上面的代码片段。

There are some free services out there that let you make country and ip-based geolocalization from the client-side.

I've used the wipmania free JSONP service, it's really simple to use:

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

Or if you use a framework that supports JSONP, like jQuery you can:

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

Check the above snippet running here.

无法言说的痛 2024-08-27 00:14:31

最好的指标可能是 HTTP Accept-Language 标头。它在 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'];
?>

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'];
?>
盛夏尉蓝 2024-08-27 00:14:31

我想说,地理定位是唯一可靠的方法。但也有一些情况根本没有帮助。我不断访问那些认为我在法国的网站,因为我公司的骨干网就在那里,所有互联网流量都经过它。

HTTP Accept 标头不足以确定用户区域设置。它仅告诉您用户选择的语言,这可能与他们所在的位置无关。有关此内容的更多信息,请参见此处

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.

走过海棠暮 2024-08-27 00:14:31

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;
    }
}
?>

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;
    }
}
?>
野稚 2024-08-27 00:14:31

@rostislav

或使用 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);
}

@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);
}
香橙ぽ 2024-08-27 00:14:31

我们只需使用 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

Simply we can use 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";} ?>

All Country codes are here..
http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2

手心的海 2024-08-27 00:14:31

我的解决方案简单而小,在本例中我使用 fr-CA 或 en-CA 语言测试加拿大地区

if( preg_match( "/^[a-z]{2}\-(ca)/i", $_SERVER[ "HTTP_ACCEPT_LANGUAGE" ] ) ){

   $region = "Canada";

}

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";

}
还给你自由 2024-08-27 00:14:31

根据您想要区分的国家/地区,时区可能是实现这一目标的一种非常简单的方法 - 我认为它非常可靠,因为大多数人都会将计算机上的时钟设置正确。 (当然,使用这种技术无法区分许多国家/地区)。

这是一个非常简单的示例,说明如何执行此操作:

http://unmissabletokyo.com/country-detector

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文