有没有快速获取用户国家代码的方法?

发布于 2024-10-13 21:47:45 字数 158 浏览 0 评论 0原文

我想知道用户是否从英国浏览或使用英国浏览器。如果不是,我可以放心地假设他们是美国游客。

我可以在数据库中查找他们的 IP,但这有点慢,我宁愿从他们的用户代理或其他一些 HTTP 标头中查找。

如果它并不总是正确的,并且存在歧义(例如有人在英国使用美国计算机),也没关系。

I want to know if the user is browsing from the UK, or using a UK browser. If not, I can safely assume that they are a US visitor.

I can lookup their IP in a database, but this is a bit slow and I'd rather just find out from their User-agent or some other HTTP header.

It doesn't matter if it's not always correct, and there are ambiguities (such as someone using a US computer in the UK).

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

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

发布评论

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

评论(2

秉烛思 2024-10-20 21:47:45

您可以检查 Accept-Language 标头字段。

14.4 接受语言

Accept-Language 请求标头
字段与 Accept 类似,但是
限制自然语言集
首选作为回应
的请求。语言标签已定义
在第 3.10 节中。

 Accept-Language = "Accept-Language" ":"
                     1#( 语言范围 [ ";" "q" "= qvalue ]

)
语言范围 = ( ( 1*8ALPHA *( "-" 1*8ALPHA ) ) | "*" )

每个语言范围可能会被赋予一个
相关质量值
表示对用户的估计
对指定语言的偏好
在这个范围内。品质价值
默认为“q=1”。例如,

 接受语言:da、en-gb;q=0.8、en;q=0.7

意思是:“我更喜欢丹麦语,但会
接受英式英语和其他类型
英语。” 语言范围匹配
语言标签(如果它完全等于)
标签,或者如果它完全等于 a
标签的前缀,使得第一个
前缀后面的标记字符是
“-”。特殊范围“*”(如果存在)
在 Accept-Language 字段中,匹配
每个标签都不与任何其他标签匹配
接受语言中存在的范围
字段。

 注意:使用前缀匹配规则并不意味着
  语言标签以这样的方式分配给语言:
  总是正确的,如果用户理解某种语言
  标签,那么该用户也将理解所有带有标签的语言
  该标签是其前缀。前缀规则只是允许
  如果是这种情况,请使用前缀标签。

分配的语言质量因子
到语言标签
接受-语言领域就是质量
最长语言范围的值
在匹配的字段中
语言标签。如果没有语言 - 范围在
该字段与标签匹配,
指定的语言质量因子为 0。
如果没有 Accept-Language 标头
存在于请求中,服务器

应该假设所有语言都是
同样可以接受。如果一个
存在 Accept-Language 标头,
然后分配的所有语言
大于 0 的品质因数是
可以接受。

这可能违反隐私
用户发送的期望
Accept-Language 标头带有
完整的语言偏好
每个请求中的用户。对于一个
关于这个问题的讨论,请参阅章节
15.1.4.

由于清晰度高度依赖
对于个人用户来说,
建议客户端应用程序
做出语言选择
用户可用的偏好。如果
该选择不可用,那么
Accept-Language 头字段必须
不要在请求中给出。

 注意:当选择语言偏好时
  用户,我们提醒实施者用户不是
  熟悉上述语言匹配的细节,
  并应提供适当的指导。举个例子,用户
  可能会假设在选择“en-gb”时,他们将获得任何
  如果没有英式英语,则为英文文档。一个
  在这种情况下,用户代理可能会建议添加“en”以获取
  最佳匹配行为。

You could probably check for Accept-Language header field.

14.4 Accept-Language

The Accept-Language request-header
field is similar to Accept, but
restricts the set of natural languages
that are preferred as a response to
the request. Language tags are defined
in section 3.10.

   Accept-Language = "Accept-Language" ":"
                     1#( language-range [ ";" "q" "=" qvalue ]

)
language-range = ( ( 1*8ALPHA *( "-" 1*8ALPHA ) ) | "*" )

Each language-range MAY be given an
associated quality value which
represents an estimate of the user's
preference for the languages specified
by that range. The quality value
defaults to "q=1". For example,

   Accept-Language: da, en-gb;q=0.8, en;q=0.7

would mean: "I prefer Danish, but will
accept British English and other types
of English." A language-range matches
a language-tag if it exactly equals
the tag, or if it exactly equals a
prefix of the tag such that the first
tag character following the prefix is
"-". The special range "*", if present
in the Accept-Language field, matches
every tag not matched by any other
range present in the Accept-Language
field.

  Note: This use of a prefix matching rule does not imply that
  language tags are assigned to languages in such a way that it is
  always true that if a user understands a language with a certain
  tag, then this user will also understand all languages with tags
  for which this tag is a prefix. The prefix rule simply allows the
  use of prefix tags if this is the case.

The language quality factor assigned
to a language-tag by the
Accept-Language field is the quality
value of the longest language- range
in the field that matches the
language-tag. If no language- range in
the field matches the tag, the
language quality factor assigned is 0.
If no Accept-Language header is
present in the request, the server

SHOULD assume that all languages are
equally acceptable. If an
Accept-Language header is present,
then all languages which are assigned
a quality factor greater than 0 are
acceptable.

It might be contrary to the privacy
expectations of the user to send an
Accept-Language header with the
complete linguistic preferences of the
user in every request. For a
discussion of this issue, see section
15.1.4.

As intelligibility is highly dependent
on the individual user, it is
recommended that client applications
make the choice of linguistic
preference available to the user. If
the choice is not made available, then
the Accept-Language header field MUST
NOT be given in the request.

  Note: When making the choice of linguistic preference available to
  the user, we remind implementors of  the fact that users are not
  familiar with the details of language matching as described above,
  and should provide appropriate guidance. As an example, users
  might assume that on selecting "en-gb", they will be served any
  kind of English document if British English is not available. A
  user agent might suggest in such a case to add "en" to get the
  best matching behavior.
太阳公公是暖光 2024-10-20 21:47:45

我可以在数据库中查找他们的 IP,但这有点慢

不,不是。


您可以使用 Accept-Language HTTP 标头 检测首选语言。

I can lookup their IP in a database, but this is a bit slow

No, it's not.


You could use Accept-Language HTTP header to detect preferred language.

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