# Which country did a user request come from? How can I determ

**URL:** <https://www.ruby-forum.com/t/which-country-did-a-user-request-come-from-how-can-i-determ/82371>\
**Category:** Rails\
**Created:** [January 19, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/which-country-did-a-user-request-come-from-how-can-i-determ/82371 "2007-01-19T16:29:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [January 19, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/which-country-did-a-user-request-come-from-how-can-i-determ/82371/1 "2007-01-19T16:29:53Z")

</div>

Hi,

Can anybody recommend a means to determine the country that an incoming  
user  
request is originating from?

I’m guessing that you would first get the IP from the HTTP header and  
then  
somehow translate this? Is there a service that can be used for this?  
Is  
there a set of rules that one can use to implement within your rails  
application? Any advice welcome, even if the best possible is only a  
95%  
accuracy thing.

Thanks in advance  
Greg

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/which-country-did-a-user-request-come-from-how-can-i-determ/82371/2 "2007-01-19T16:31:13Z")

</div>

PS Meant to ask too whether a rails plugin exists for this, and whether  
this can be done for free (i.e. without having to pay to subscribe to a  
service).

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 25, 2007, 10:38pm UTC](https://www.ruby-forum.com/t/which-country-did-a-user-request-come-from-how-can-i-determ/82371/3 "2007-09-25T22:38:37Z")

</div>

Yap, use GeoIP gem (look in [www.gemjack.com](http://www.gemjack.com)). the free version will  
reply to you with a country.

works great, i use it myself in one project.

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 25, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/which-country-did-a-user-request-come-from-how-can-i-determ/82371/4 "2007-09-25T22:40:35Z")

</div>

Any idea why the following fails?

$ ls -l /usr/share/GeoIP/GeoIP.dat  
-rwxr-xr-x 1 root wheel 682474 Jan 13 09:17  
/usr/share/GeoIP/GeoIP.dat

$ cat a.rb  
require ‘geoip’  
p GeoIP.new(’/usr/share/GeoIP/GeoIP.dat’).country(“www.netscape.sk”)

$ RUBYOPT=rubygems ruby a.rb  
/usr/lib/ruby/gems/1.8/gems/geoip-0.3.0/lib/geoip.rb:273:in  
`getaddress': getaddrinfo: No address associated with nodename (SocketError) from /usr/lib/ruby/gems/1.8/gems/geoip-0.3.0/lib/geoip.rb:273:in`country’  
from a.rb:2

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 25, 2007, 10:46pm UTC](https://www.ruby-forum.com/t/which-country-did-a-user-request-come-from-how-can-i-determ/82371/5 "2007-09-25T22:46:17Z")

</div>

(Replying to self). The problem is that www.netscape.sk does not  
exist. The code works with existing sites. Sorry for the confusion…

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [September 25, 2007, 11:11pm UTC](https://www.ruby-forum.com/t/which-country-did-a-user-request-come-from-how-can-i-determ/82371/6 "2007-09-25T23:11:11Z")

</div>

thanks for the reply Elad - I’ll have a look right now - how accurate do  
you  
find [www.gemjack.com](http://www.gemjack.com) to be?
