User IP

A user IP is a valuable identifier. IPs generally identify one user–though they may also identify several users behind one network (eg. several employees behind one company firewall). That begs the question–how do you fetch the current user’s IP in Rails?

The answer is in the request variable, which is available for free to all your controllers. You can fetch the current user’s IP from request.env['REMOTE_ADDR'].

What’s more, if you ever need to fake a user IP–such as when testing your IP-blocking code–you can simply set the variable yourself. Fantastic!

About Ashiq Alibhai

Ashiq Alibhai, PMP, has been a Rails aficionado since 2007, and developed web applications since early 2003, where he learned PHP in one summer. As the driving-force behind RailsRocket and the Launchpad project, he seeks to share the ease of development with Rails far and wide.
This entry was posted in Development and tagged , , . Bookmark the permalink.

Comments are closed.