Skip to content Skip to sidebar Skip to footer

Is It Possible To Run Code If Visitor Is From A Certain Country

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?

Solution 1:

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.

Solution 2:

If you are able to detect user language / country server side, you may assign that information as a variable in javascript so you can run specific function then. Something more?

Solution 3:

It totally depends on your requirements and use-case. You can go for implementing your own ip-to-country API. This can be done using this service without reinventing the wheel:

enter image description here

Disclosure: I am a part of their team.

Post a Comment for "Is It Possible To Run Code If Visitor Is From A Certain Country"