开发者

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

开发者 https://www.devze.com 2023-03-20 16:41 出处:网络
I want to log user\'s ip address, referer, and user agent. In PHP, I can get them from the following variables:

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?


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


You need the array request.env

request.env['REMOTE_ADDR']:


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

0

精彩评论

暂无评论...
验证码 换一张
取 消