如果访问者来自某个国家,是否可以运行代码? [英] Is it possible to run code if visitor is from a certain country

查看:121
本文介绍了如果访问者来自某个国家,是否可以运行代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果且仅当访问者来自某个国家时,我想运行一段代码,这是否可能,我将如何去做这件事?

解决方案

这是不可能的,但有一些东西可以用来作为访问者的原籍国的提示:


  • Accept-Language HTTP头告诉你(如果有的话)用户喜欢看哪种语言 - 当然用户可以将它设置为任何他们想要的,语言不会将1:1映射到国家。

  • 还有一些 Javascript对象,告诉你用户的浏览器和操作系统使用哪种语言;与上述相同的注意事项。

  • 有许多 IP地理位置服务,您可以使用该服务从用户的IP地址派生地理位置。但这些有时可能不会产生任何结果或错误结果,我不确定它们中的任何一个是否可以自由使用。


I want to run a bit of code if, and only if, a visitor is from a certain country, is this possible and how would i go about doing this?

解决方案

It's not possible with any certainty, but there are a couple of things that can be used as hints towards a visitor's country of origin:

  • The Accept-Language HTTP header tells you (if present) which language the user prefers to see - of course users can set this to whatever they want, and languages do not map 1:1 to countries.
  • There are also a number of Javascript objects that tell you which language the user's browser and OS use; same caveats as above.
  • There are a number of IP Geolocation services that you can use to derive a geographic location from the user's IP address. But these can sometimes yield no results or false results, and I'm not sure whether any of them are free to use.

这篇关于如果访问者来自某个国家,是否可以运行代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆