How to get user's IP

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 S.

Pawel S. wrote:

Regards
Pawel S.

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.

Hi Pawel,

Pawel S. wrote:

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:

Rails is probably already logging everything you’re looking for in
#{RAILS_ROOT}/log/production.log. Every request/reponse cycle generates
an
entry containing the time of the request, the requesting IP address, the
session id, controller / action requested, parameters passed, and
response.
You might want to consider using the session id rather than IP address
to
track visitors. If the visitor is using AOL, for example, the IP
address
you see can change during their visit. I haven’t tested it, but I think
Rails will maintain the same session ID throughout.

If you’re in development mode, your development.log will contain more
info
than your production.log will when you switch to that environment. If
you
want to see what you’ll be getting when you move to production, just
uncomment the line (around line 27) in
#{RAILS_ROOT}/config/environment.rb
that reads “#config.log_level = :debug” and change it to
“config.log_level =
:info”.

Hope that helps,
Bill

Thanks for all the answers.

I think I will use some logging because I want to log what user does in
my application. This can be all retrieved from actions and parameters
data, but I prefer to have it logged in more human-readable way. I know
IP is not perfect, but without user login it is the best I can have.

Regards
Pawel S.

On 22 Jun 2007, at 15:19, Pawel S. wrote:

I think I will use some logging because I want to log what user
does in
my application. This can be all retrieved from actions and parameters
data, but I prefer to have it logged in more human-readable way. I
know
IP is not perfect, but without user login it is the best I can have.

You’ll be better of writing a log analyzer (and visualizer) than a
method call and another hit on your db on each request. The logs
aren’t that difficult to parse and you can parse the whole lot during
the night and generate staticly cached pages.

Best regards

Peter De Berdt

ip_addr = request.env[‘REMOTE_ADDR’]

also:

request.remote_ip

Lots of good info available here:

http://api.rubyonrails.com/classes/ActionController/AbstractRequest.html

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

On 6/24/07, Carl J. [email protected] wrote:

Peak Obsession

–
Robert W. Oliver II
CEO of OCS Solutions, Inc., Web Hosting and Development

Oops … sorry about the space, should be:

ip_addr = request.env[‘X_FORWARDED_FOR’]

Robert

On 6/24/07, Robert O. [email protected] wrote:

ip_addr = request.env [‘X_FORWARDED_FOR’]

Robert

–
Robert W. Oliver II
CEO of OCS Solutions, Inc., Web Hosting and Development

You’ll be better of writing a log analyzer (and visualizer) than a
method call and another hit on your db on each request. The logs
aren’t that difficult to parse and you can parse the whole lot during
the night and generate staticly cached pages.

Why DB? I want to log to file only. Thanks anyway.

Regards
Pawel S.

Robert O. wrote:

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.

That’s why I recommended request.remote_ip. It returns HTTP_CLIENT_IP or
X_FORWARDED_FOR if present, otherwise REMOTE_IP. No need for you to
repeat what Rails already does for you. :slight_smile:

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

Robert

On 6/24/07, Carl J. [email protected] wrote:

That’s why I recommended request.remote_ip. It returns HTTP_CLIENT_IP or
X_FORWARDED_FOR if present, otherwise REMOTE_IP. No need for you to
repeat what Rails already does for you. :slight_smile:

–
Robert W. Oliver II
CEO of OCS Solutions, Inc., Web Hosting and Development

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?

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