Getting a user country name from originating IP address with Ruby on Rails

You can use geoip gem. environment.rb config.gem ‘geoip’ Download GeoIP.dat.gz from http://www.maxmind.com/app/geolitecountry. unzip the file. The below assumes under #{RAILS_ROOT}/db dir. @geoip ||= GeoIP.new(“#{RAILS_ROOT}/db/GeoIP.dat”) remote_ip = request.remote_ip if remote_ip != “127.0.0.1” #todo: check for other local addresses or set default value location_location = @geoip.country(remote_ip) if location_location != nil @model.country = location_location[2] end end

How does IP geolocating work?

Mapping IP addresses to geolocations is done via tables, where an IP maps to a particular location. This location, however, doesn’t need to be accurate, since IP addresses don’t carry any information about their locations, these are approximated. From Wikipedia’s article on Internet geolocation: The primary source for IP address data is the regional Internet … Read more

Get Country of IP Address with PHP [duplicate]

There are free, easy APIs you can use, like those: http://ipinfodb.com/ip_location_api.php http://www.ipgeo.com/api/ http://ip2.cc/ http://www.geobytes.com/IpLocator.htm https://iplocate.io/ and plenty others. Which one looks the most trustworthy is up to you 🙂 Otherwise, there are scripts which are based on local databases on your server. The database data needs to be updated regularly, though. Check out this one: … Read more