2

I am looking at trying to access the country the user is accessing my site from e.g. France, Uk etc... and am looking at the best possible and most accurate way to achieve this.

I started on this problem by using $locale = new Zend_Locale(Zend_Locale::BROWSER); but that gave me en_US. Im in the UK using a virtual host (I have read up on the Zend_Locale function and now understand the reason it returns this result). I have considered some sort of HTML5 geolocation option, but if the user has javascript turned off, i'm assuming it wont work and as you can probably tell from that last comment, my Javascript is non-existent so I think thats not an option (short of spending the next few weeks or months learning a new language).

Finally I was considering using $_SERVER['REMOTE_ADDR']; then comparing the returned Ip address with some free service. However I was wondering if there is a better way in Magento, or if this way is accurate enough?

It is not vitally important that it is correct 100% of the time, however it would be nice if I can get it close to that.

tony09uk
  • 1,707
  • 11
  • 41
  • 56

4 Answers4

2

GeoIP from MaxMind is a popular database for this (unfortunately not for free). You can obtain the country, region, city and organisation from it.

There is an extension on Magento Connect, honestly I have to say I never used this extension, but used the GeoIP functionallity from another extension which also uses the GeoIP data.

For retrieving the data, you need to download GeoLiteCity.dat file from the website and put it in your /media/geoip/ directory.

Anna Völkl
  • 17,341
  • 5
  • 60
  • 141
  • I've used it - it's Tim's extension (moderator on this site). Works great. One thing I got told was that the Maxmind list only got updated once a month (on the 26th) not sure if that's true or not. – McNab Sep 25 '13 at 22:00
1

If I need a free solution I normally user IpInfoDB. Their API is easy to use, is available in several formats (plain text, JSON) and the service is pretty accurate.

I don't think there's an extension on Magento Connect but I have some code on Github that you can use as a starting of point, it's a model with a function that calls the API

Sander Mangel
  • 37,528
  • 5
  • 80
  • 148
1

I think the most accurate way would be to ask the user.

I can tell you that in my experience as a user, I find it incredibly frustrating when sites attempt to guess my location only to get it wrong. I am generally using a WiMax connection for internet, and am told by many sites that I am visiting from Salt Lake City instead of Denver (where I am). Google does this and Bing does this, and since their map tools are popular on a variety of sites, every time one loads it points me to the wrong location (ie a "find a store" feature that automatically zooms me into a city I do not live in).

If you want to set this kind of thing automatically for a user, you should also provide them a simple method for setting it themselves, since that will be the most accurate.

pspahn
  • 4,648
  • 3
  • 32
  • 58
0

Im doing something very similar at the moment and there is a magento extension GeoIp but used the github version as it was more up to date.

I've created a magento module that uses geoip to redirect users to a different store based on their location, eg AUS customers who try to go to the UK store will get redirected back to the AUS store.

rob3000
  • 2,212
  • 13
  • 26