Rails 3 - IP 地址的获取和比较
我通过 找到google 我的 IP 地址,现在我尝试根据 IP 地址进行重定向。
对于 HomeController#index 我设置了以下内容:
unless request.remote_ip.to_s == '1.2.3.4'
redirect_to 'http://www.some_page.com'
return
end
但这不起作用,因为我总是在 *some_page.com* 上重定向...我做错了什么?
I found through google my IP address and now I try to make a redirect based on the IP address.
To the HomeController#index I set followings:
unless request.remote_ip.to_s == '1.2.3.4'
redirect_to 'http://www.some_page.com'
return
end
But this doesn't works, because I am always redirected on the *some_page.com*... What I am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你在本地机器上尝试这个吗?那么从服务器的角度来看,您的“远程IP”将始终是127.0.0.1,不是你的外部IP。
不管怎样,看看 request.remote_ip 实际返回了什么。也许它会带来一些启发。
Are you trying this on a local machine? Then your "remote IP" from the server's point of view would always be 127.0.0.1, not your external IP.
Anyways, see what
request.remote_ip
is actually returning. Maybe it'll shed some light.