How to get user's IP

Pawel Stawicki wrote:

Hello,

I want to log users who enter my site and what they are doing.
Registration and login is not required, so what is left is IP.
(something like wikipedia, you can edit without logging in, and then
there is your IP in log). How to get this IP in Ruby on Rails?

And if you know some nice tutorial about logging in ruby I would also
appreciate :wink: I mean logging events to some file, not logging users to
service :slight_smile:

Regards
Pawel Stawicki
  
A google search for this 'rails ip address of client' returned this:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/238784
which gave this:

ip_addr = request.env['REMOTE_ADDR']

Sorry, can't answer the second question :slight_smile:
Cheers,
Mohit.
6/22/2007 | 7:53 PM.

Just want to mention also that if you are behind a load balancer, the REMOTE_ADDR may return the load balancer’s IP, not the actual client’s. In this case, you can typically use something like:

ip_addr = request.env
[‘X_FORWARDED_FOR’]

Robert

Oops … sorry about the space, should be:

ip_addr = request.env[‘X_FORWARDED_FOR’]

Robert

Nice, I wasn’t aware of that. Thanks!

Robert

Im using request.remote_ip now but the problem is if im behind a proxy i
always get ip as 127.0.0.1

But in the development.log i can see the correct ip like this
Processing AuthController#login (for 192.168.1.4 at 2007-07-05 11:31:17)
[POST]

Is there a way to get the ip from the log or some other workaround?

Spit out the contents of request.env (a hash) and see if what you want is in there. Usually there's an X-header that will have the clients ip...

-philip