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

烈酒焚心 提交于 2019-12-04 06:23:37
timaschew

You need the array request.env

request.env['REMOTE_ADDR']:

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

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

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