In my application there is a need to track the ip address of the user. can anybody tell how to do that..
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
Hi thanks for reply..
Its working.. and retrieing ip of my localhost
I need the ip address of internet connection and hostname.
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.
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.