How to get ip address, referer, and user agent in ruby?

此生再无相见时 提交于 2019-12-06 01:24:10

问题


I want to log user's ip address, referer, and user agent.
In PHP, I can get them from the following variables:

$_SERVER['REMOTE_ADDR']
$_SERVER['HTTP_REFERER']
$_SERVER['HTTP_USER_AGENT']

How to get them in ruby?


回答1:


You need the array request.env

request.env['REMOTE_ADDR']:




回答2:


PHP is embedded in a web server. Ruby is a general-purpose language: if you need a web server context, you'll have to install it yourself. Fortunately, it's easy.

One of the easiest ways to get started is with Sinatra. Install the gem:

gem install sinatra

Then create myapp.rb:

require 'sinatra'

get '/' do
    request.user_agent
end

Start up the web server:

ruby -rubygems myapp.rb

Visit Sinatra's default URL: http://localhost:4567/

Et voilà.




回答3:


I'm assuming you by ruby you mean ruby on rails, the following link shows you how to access them:

http://techoctave.com/c7/posts/25-rails-request-environment-variables



来源:https://stackoverflow.com/questions/6701577/how-to-get-ip-address-referer-and-user-agent-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!