生成和使用所有的IP地址与PHP [英] generating and using all IP addresses with php

查看:165
本文介绍了生成和使用所有的IP地址与PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对java一无所知(我认为这是针对java的这个主题的一个线程),还有一点关于php的问题,我想知道如何使用php 生成一个完整的所有列表可能的IP地址(0.0.0.0到255.255.255.255),然后我怎样才能在php脚本中使用这些脚本来测试我正在尝试设置的IP验证工具(将其中的每一个用于使用它) 。



对于问题的第二部分,我正在考虑使用foreach语句或使用循环内的测试代码部分来生成每个IP组合,以便在IP生成时进行测试。

然后对于第一部分,我不得不承认,我被卡住了,而且我的基本知识没有让我找到一个令人满意的解决方案
到目前为止,我已经与这个:

pre $功能gen(){
$ n1 = 0;
$ n2 = 0;
$ n3 = 0;
$ n4 = 0; ($ n4 <= 255&& $ n3< = 255){
echo $ n1。'。'。$ n2。'。'。$ n3。

。 ' '$ N4 ++。' <峰; br>';
echo $ n1。'。'。$ n2。'。'。$ n3 ++。'。'。$ n4。'< br>';


gen();

这只是部分地做我想要的。


以下正则表达式将允许您验证任何特定的IPv4地址从0.0.0.0到255.255。 255.255

\b(25 [0-5] | 2 [0-4] [0-9] | [01]?[ 0-9] [0-9])\(25 [0-5] | 2 [0-4] [0-9] |?[01] [0-9] [0-9]) \。(25 [0-5] | 2 [0-4] [0-9] | [01] [0-9] [0-9]?)\(25 [0-5]。| 2 [0-4] [0-9] | [01]?[0-9] [0-9]?)\b



然而,你需要记住的是,并不是所有地址组合都归类为有效,即使它们的语法正确。

另外一种选择由@binaryLV提到)正在使用PHP filter_var($ ip,FILTER_VALIDATE_IP);功能很好,干净。检查标志,以帮助过滤在 http://php.net/manual /en/function.filter-var.php



更新

<对于基于IP位置的服务,我建议使用诸如 IPInfoDB 的服务。他们提供 API(请参阅此处)使用,使您可以调用其服务,减少存储的需要你的数据库上的信息。否则,他们提供一个 IP地址数据库(见这里)到xxx.xxx.xxx精度,也可以下载。


knowing nothing about java (I thought a thread on this same topic for java) and a tiny bit about php, I was wondering how I could generate, with php, a complete list of all the possible IPs (0.0.0.0 to 255.255.255.255) and then how I can use each of those in a php script that is intended to test a IP verification tool that I am trying to set up (take each one of them and use it).

For the second part of the question, I was thinking about using a foreach statement or using the part of the "testing code" inside the loop that would generate each IP combination, so that it is tested as the IPs are generated.

Then for the first part, I have to admit that I am stuck and that my basic knowledge doesn't enable me to find a satisfying solution So far I have come with this:

function gen() {
    $n1 = 0;
    $n2 = 0;
    $n3 = 0;
    $n4 = 0;

    while ($n4 <= 255 && $n3 <= 255) {
        echo $n1.'.'.$n2.'.'.$n3.'.'.$n4++.'<br>';
        echo $n1.'.'.$n2.'.'.$n3++.'.'.$n4.'<br>';
    }
}
gen();

which just partially does what I want.

解决方案

Rather than generating a possible database / storage for ~4 billion IP4 addresses (assuming you are only looking at IPv4 ignoring all IPv6 addresses) would it not be easier, and more practical just generate random IP address combinations to test with, which you can easily validate with RegEx?

The following regex would allow you to validate any particular IPv4 address from 0.0.0.0 to 255.255.255.255

\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b

What you do need to remember however is that not all combinations of addresses are classed as 'valid' even if they are syntax correct.

Another alternative (as also mentioned by @binaryLV) is using the PHP filter_var($ip, FILTER_VALIDATE_IP); function which is nice and clean. Check the flags out to help filter too over at http://php.net/manual/en/function.filter-var.php

Update

For IP location based services I would recommend using a service such as IPInfoDB. They provide both an API (see here) to use which allows you to call their service reducing the need to store the information on your database. Else they offer a database of IP addresses (see here) to xxx.xxx.xxx precision that can also be downloaded.

这篇关于生成和使用所有的IP地址与PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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