Browse the Ruby on Rails Community.

You are here: Forums Ask a Rails expert tracking ip address...

Replytotopic

tracking ip address

Posted in Forums : Ask a Rails expert

 
Profile

Authority 37
Posting Rating 64
Sign in to rate this post

Hi all

In my application there is a need to track the ip address of the user.
can anybody tell how to do that..

 
Profile

Authority 25
Posting Rating 68
Sign in to rate this post

Hi. In your Rails controller you have access to a method named “remote”. This gives you access to most of the information associated with a web request, including the IP address.

For the IP adress you simply then do: “request.remote_ip”. Pretty easy :)

More info on ActionController::AbstractRequest
http://api.rubyonrails.org/classes/ActionController/AbstractRequest.html

 
Profile

Authority 37
Posting Rating 64
Sign in to rate this post

Hi thanks for reply..

Its working.. and retrieing ip of my localhost

I need the ip address of internet connection and hostname.

 
Profile

Authority 25
Posting Rating 68
Sign in to rate this post

Is your server running behind some sort of proxy setup? In that case it might be more tricky to get the IP.

If you look at the source code for remote_ip in AbstractRequest:
http://api.rubyonrails.org/classes/ActionController/AbstractRequest.html#M000802
(click “show source”)

..you will see that it tries to look in several different ENV variables. You can try to output these in your controller and see if one of the env variables contains the remote IP you are looking for.

For example in your controller do:
logger.info(request.env.inspect)

Then look in your logfile and see if the remote ip is stored in one of the env-variables, then use that one. For example:
@remote_ip = request.env[“HTTP_X_FORWARDED_FOR”]

You will have to do a bit of detective work, but hopefully the IP is in there somewhere.

 
Blackburn_120x180

Authority 62
Posting Rating 89
Sign in to rate this post

In any case, there is not an easy and reliable way to determine a user’s IP address. The information can certainly be forged and especially if the user is behind a chain of anonymizing proxies. However, if you don’t need to trust that it is always correct Casper has pointed you to the best-practice solution above.

Replytotopic

Other Recent Topics

Ask a Rails expert : Ruby Builder

Ask a Rails expert : Better programming skills

Ask a Rails expert : Select Option

Ask a Rails expert : Updating ruby version on windows

Ask a Rails expert : Periodicall Ajax Calls and Session Expiry

Ask a Rails expert : Sub queries in RoR

Ask a Rails expert : Remembering associations

Ask a Rails expert : Keeping the databse clean

Ask a Rails expert : How to redirect single app to 2 different URLs

Ask a Rails expert : Dealing with nested models

Formatting Help
  • *bold*       _italics_      
    bq. (quotes)
  • "DSC":http://www.dsc.net
  • * or # (lists)
or cancel