如何确定用户IP地址是否在可访问范围内? [英] How can I determine that user IP address is in accessible range or not?

查看:193
本文介绍了如何确定用户IP地址是否在可访问范围内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在抓取用户的IP地址,然后我想确定该IP地址是否在可访问的范围内。

I am grabbing user IP address, and then I want to determine whether that IP address is in accessible range or not.

我正在做的是,在我的PHP脚本我正在使用' ip2long '函数并将其与允许的ip地址范围进行比较。

What I am doing is, in my PHP script i am using 'ip2long' function and compare it with allowed range of ip address.

我知道这不是一个干净的方法吗?有没有人有更好的想法呢?

I know this is not a clean way to do that...does anyone have better idea to do that?

任何帮助表示赞赏。

谢谢,
Amit

Thanks, Amit

推荐答案

至少,您需要将前N位与已知私有或已知未使用的网络块进行比较。最低限度是 RFC 1918私人网络:

At minimum, you need to be comparing the top N bits against known-private or known-unused network blocks. And the minimum of that is the RFC 1918 private networks:

10/8
172.16/12
192.168/16

(这种表示法意味着如果你屏蔽前8位并获得10,它就在第一个私有块中.12位== 172.16是第二位,等等)

(This notation means that if you mask off the top 8 bits and get "10", it's in the first private block. 12 bits == 172.16 is the second, etc.)

你可以检测到更多的块。

There are many more blocks you could detect.

最简单的例子是127/8,这些都是环回地址。您可能知道127.0.0.1,但事实上所有127.xyz地址都指的是您机器上的环回接口。

The easiest example is 127/8, which are all loopback addresses. You probably know about 127.0.0.1, but in fact all 127.x.y.z addresses refer to the loopback interface on your machine.

一个更加模糊的例子是5/8,其中已分配,但从未在广泛的互联网上使用,因此它被 Hamachi 所占用。但是,如果您的程序可以在可以访问Hamachi网络的计算机上运行,​​您可能需要小心测试。

A more obscure example is 5/8, which was assigned but never used on the wide Internet, so it's been appropriated by Hamachi. You would want to be careful about testing for this, though, if there's a chance that your program could be run on a machine with access to the Hamachi network.

另一组您可能希望忽略的地址是各种多播范围。你应该只向那些人发送UDP数据包,而不是TCP连接。

Another set of addresses you'll probably want to ignore are the various multicast ranges. You should only ever be sending UDP packets to those, never TCP connections.

如果你想要变得非常积极,你可以及时了解最新发布的作业。 IANA ,忽略尚未分配的块中的IP。但是,您希望经常更新该列表。

If you want to get really aggressive, you could keep up to date on the latest assignments published by IANA, ignoring IPs from blocks that haven't been assigned yet. You'd want to keep that list frequently updated, though.

这篇关于如何确定用户IP地址是否在可访问范围内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆