Detect Country then show message?

283 views Asked by At

I have a US and UK website. If the user is from the UK, and is on the US website, then show a message on a page to redirect to the correct site. How can I do this?

2

There are 2 answers

0
Ted Nyberg On

You can look up the IP in a geo database, such as GeoLite (http://dev.maxmind.com/geoip/legacy/geolite), to figure out where the user is located.

GeoLite databases can be downloaded to your app. Then simply create a small server endpoint to make IP lookups in the database.

Using JavaScript you can then:

  1. Grab the IP of the current user
  2. Look up the location in the GeoLite database through the server endpoint
  3. If in UK on US site (or vice versa) -> display message and optionally redirect the user to the correct website

Of course you could also do this server-side, and automatically redirect the user if that's your preferred approach.

0
Tikeb On

One option is to get the IP and use a geo-location lookup as discussed in this thread? Once you know the IP you can get the country and redirect as required.