Rails 3 - IP 地址的获取和比较

发布于 2024-12-12 06:05:04 字数 411 浏览 2 评论 0原文

我通过 找到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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独行侠 2024-12-19 06:05:04

你在本地机器上尝试这个吗?那么从服务器的角度来看,您的“远程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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文