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

2019-04-29 12:56发布

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?

3条回答
做个烂人
2楼-- · 2019-04-29 13:28

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

查看更多
虎瘦雄心在
3楼-- · 2019-04-29 13:31

You need the array request.env

request.env['REMOTE_ADDR']:

查看更多
SAY GOODBYE
4楼-- · 2019-04-29 13:39

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

查看更多
登录 后发表回答