我想检查一个设备的ip地址出现在我的网站(root to=>home#index)的次数,如果这个地址出现在我的网站上超过一次,然后执行一段代码(任何东西).How我用rails实现了吗?换句话说,我如何访问并设置一个变量来存储登录到index action中的用户的数量?根页面是home#index.To澄清更多的用户不必是注册用户,我只想跟踪计算机的ip地址。我需要帮助在索引action.There是设计current_sign_in_ip注释掉的词,但我不确定这是否意味着解决这个问题的情况,因为我是非常新的设计和rails在general.Thank你这么多!
控制器
class HomeController < ApplicationController
def index
#access user ip address
#set a variable to increment number of times the address come to a site
# if no_of_ip_login is_greater_than_1 #In other words if it is a returning user
redirect_to :controller=>"home" ,:action=>"show"
end
end发布于 2011-11-05 03:52:08
您可以使用
ip_addr = request.env['REMOTE_ADDR']另外:
request.remote_ip还想提一下,如果你在负载均衡器后面,REMOTE_ADDR可能会返回负载均衡器的IP,而不是实际客户端的IP。在这种情况下,你通常可以使用如下内容:
ip_addr = request.env['X_FORWARDED_FOR']所以我推荐了request.remote_ip。如果存在,则返回HTTP_CLIENT_IP或X_FORWARDED_FOR,否则返回REMOTE_IP。
https://stackoverflow.com/questions/8014867
复制相似问题