在 PHP 中检测客户端语言环境的最简单方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/297542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simplest way to detect client locale in PHP
提问by nickf
I would like to be able to detect what country a visitor is from on my website, using PHP.
我希望能够使用 PHP 在我的网站上检测访问者来自哪个国家/地区。
Please note that I'm not trying to use this as a security measure or for anything important, just changing the spelling of some words (Americans seems to believe that the word "enrolment" has 2 Ls.... crazy yanks), and perhaps to give a default option in a "Select your country" list.
请注意,我不是想将其用作安全措施或任何重要的事情,只是更改某些单词的拼写(美国人似乎认为“注册”一词有 2 Ls .... crazy yanks),并且也许在“选择您的国家/地区”列表中提供默认选项。
As such, using a Geolocation database is a tad over-the-top and I really don't want to muck about with installing new PHP libraries just for this, so what's the easiest/simplest way to find what country a visitor is from?
因此,使用地理定位数据库有点过分,我真的不想为此安装新的 PHP 库,那么查找访问者来自哪个国家/地区的最简单/最简单的方法是什么?
采纳答案by JasonTrue
Not guaranteed, but most browsers submit an Accept-Language HTTP header that specifies en-us if they're from the US. Some older browsers only said they are en, though. And not all machines are set up correctly to indicate which locale they prefer. But it's a good first guess.
不能保证,但大多数浏览器提交 Accept-Language HTTP 标头,如果它们来自美国,则指定 en-us。不过,一些较旧的浏览器只说它们是 en。并非所有机器都正确设置以指示他们喜欢哪种语言环境。但这是一个很好的初步猜测。
English-UK based-users usually set their system or user locale to English-UK, which in default browser configurations should result in en-gb as the Accept Language header. (An earlier version of this said en-uk; that was a typo, sorry.) Other countries also have en locales, such as en-za (south africa), and, primarily theoretically, combinations like en-jp are also possible.
基于英语-英国的用户通常将他们的系统或用户区域设置为英语-英国,这在默认浏览器配置中应导致 en-gb 作为接受语言标题。(早期版本说 en-uk;这是一个错字,抱歉。)其他国家/地区也有 en 语言环境,例如 en-za(南非),并且主要从理论上讲,像 en-jp 这样的组合也是可能的。
Geo-IP based guesses will less likely be correct on the preferred language/locale, however. Google thinks that content-negotiation based on IP address geolocation makes sense, which really annoys me when I'm in Japan or Korea...
然而,基于地理 IP 的猜测在首选语言/区域设置中不太可能正确。Google 认为基于 IP 地址地理定位的内容协商是有道理的,这让我在日本或韩国时真的很烦...
回答by Trevor Goodyear
PHP provides a function since 5.3.0 to parse the $_SERVER['HTTP_ACCEPT_LANGUAGE']variable into a locale.
PHP 自 5.3.0 起提供了一个函数来将$_SERVER['HTTP_ACCEPT_LANGUAGE']变量解析为语言环境。
$locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
echo $locale; // returns "en_US"
Documentation: https://www.php.net/manual/en/locale.acceptfromhttp.php
回答by chroder
You can check out the HTTP_ACCEPT_LANGUAGEheader (from $_SERVER) that most browsers will send.
您可以查看大多数浏览器将发送的HTTP_ACCEPT_LANGUAGE标题(来自$_SERVER)。
Take a look at Zend_Localefor an example, or maybe you might even want to use the lib.
以Zend_Locale为例,或者您甚至可能想要使用该库。
回答by Patrick Desjardins
You can do some IP comparaison withouthaving a whole library to do it.
您可以进行一些 IP 比较,而无需整个库来进行。
Solution #1
解决方案#1
Use an API, this way nothing is needed from your side. This is a web API that let you know the country:
使用 API,这样您就不需要任何东西了。这是一个让您了解国家/地区的 Web API:
Example: http://api.hostip.info/get_html.php?ip=12.215.42.19
Return : Country: UNITED STATES (US)
Solution #2
解决方案#2
But, Have you think to use the browser agent language? You might be able to know the type of english from it.
但是,你有没有想过使用浏览器代理语言?您也许可以从中了解英语的类型。
Solution #3
解决方案#3
This website called BlockCountrylet you have a list of IP by country. Of course, you do not want to block, but you can use the list of IP and compare them (get all US IP...) this might not be accurate...
这个名为BlockCountry 的网站可让您按国家/地区列出 IP。当然,您不想阻止,但是您可以使用 IP 列表并进行比较(获取所有美国 IP...)这可能不准确...
回答by Dave Sherohman
Given your stated purpose, the Accept-Language header is a more suitable solution than IP-based geolocation. Indeed, it's precisely the intended purpose of Accept-Language.
鉴于您陈述的目的,Accept-Language 标头是比基于 IP 的地理定位更合适的解决方案。事实上,这正是 Accept-Language 的预期目的。
回答by Slipo
Parse $_SERVER["HTTP_ACCEPT_LANGUAGE"]to get country and browser's locale.
解析$_SERVER["HTTP_ACCEPT_LANGUAGE"]以获取国家/地区和浏览器的区域设置。
回答by CMS
回答by Mircea Stanciu
I use the HTTP_ACCEPT_LANGUAGE
我使用 HTTP_ACCEPT_LANGUAGE
$localePreferences = explode(",",$_SERVER['HTTP_ACCEPT_LANGUAGE']);
if(is_array($localePreferences) && count($localePreferences) > 0) {
$browserLocale = $localePreferences[0];
$_SESSION['browser_locale'] = $browserLocale;
}
回答by Greg Hewgill
The http://countries.nerd.dkservice is what I use for IP-to-country mapping. It works really well and being based on DNS, is cached well too.
该http://countries.nerd.dk服务是我使用的IP到国家的映射。它工作得非常好,并且基于 DNS,缓存也很好。
You can also download the database for local use if you don't want to rely on an external service.
如果不想依赖外部服务,也可以下载数据库供本地使用。
回答by pakaian bayi
GeoIP extension is good choice.
GeoIP 扩展是不错的选择。

