Interested in making a PHP script that increments IP address from defined starting address to defined ending address

I know I can do this easily by converting the IP addresses to decimal notation first using PHP built in functions like up2long and long2ip. I just want to be able to do the same using the standard IP address notation as an exercise.

The problem I am thinking goes like this: Given an starting IP address, say 192.168.1.100, and an ending IP address, say 201.130.22.10. Make the program that prints all the address numbers in that range (192.168.1.100, 192.168.1.101, … , 201.130.22.9, 201.130.22.10).

I was thinking that maybe the way to go would be to make a nested for loop inside a while condition until the first octet of the starting address matches the first octet of the ending address. Then execute the same block of code for the second octet and so on until the program reaches the ending address and finished.

I just started learning to program recently so it is quite possible that my of thinking and or writing code is far from elegant. If you were to this, how would you do it?

Something like this:

<?php

// works only for valid range
$start_ip = '10.0.0.1';
$end_ip = '10.0.20.1';

$start_arr = explode('.',$start_ip);
$end_arr = explode('.',$end_ip);

while($start_arr <= $end_arr)
{
    echo implode('.',$start_arr) . '<br>';

    $start_arr[3]++;
    if($start_arr[3] == 256)
    {
        $start_arr[3] = 0;
        $start_arr[2]++;
        if($start_arr[2] == 256)
        {
            $start_arr[2] = 0;
            $start_arr[1]++;
            if($start_arr[1] == 256)
            {
                $start_arr[1] = 0;
                $start_arr[0]++;
            }
        }
    }
}

?>

Interested in making a PHP script that increments IP address from defined starting address to defined ending address I know I can do this easily by converting the IP addresses to decimal notation first using PHP built in functions like up2long and long2ip.

This is much less complicated:

<?php

// works only for valid range
$start_ip = ip2long('10.0.0.1');
$end_ip = ip2long('10.0.20.1');

while($start_ip <= $end_ip){
  echo long2ip($start_ip).'<br>';
  $start_ip++;
}

?>

The simplest way to collect the visitor IP address in PHP is the REMOTE_ADDR. Pass the 'REMOTE_ADDR' in PHP $_SERVER variable. It will return the IP address of the visitor who is currently viewing the webpage.

function getInBetweenIPs($startIP,$endIP){

    $subIPS = array();
    $start_ip = ip2long($startIP);
    $end_ip = ip2long($endIP);

    while($start_ip <= $end_ip){
        $subIPS[]=long2ip($start_ip);
        $start_ip++;
    }

    return $subIPS;
}

By looking at the allotment list we can find out the whois server of an ip address, and then query the corresponding ip address for the whois data. IPs which have not been allotted to any RIR, will have their whois information at whois.iana.org. To fetch the whois data of an IP address , the steps are as follows. 1.

Increment (Add to):

<?php

function ipinc($i): string {
    $i = explode(".", $i);

    $a = $i[0];
    $b = $i[1];
    $c = $i[2];
    $d = $i[3];

    $d++;

    if ($d > 255) {
        $d = 0;
        $c++;
    }

    if ($c > 255) {
        $c = 0;
        $b++;
    }

    if ($b > 255) {
        $b = 0;
        $a++;
    }

    if ($a > 255) {
        die("IPv4 Range Exceeded");
    }

    return "$a.$b.$c.$d";
}

?>

Decrement (Take from):

<?php

function ipdec($i) {
    $i = explode(".", $i);

    $a = $i[0];
    $b = $i[1];
    $c = $i[2];
    $d = $i[3];

    $d--;

    if ($d < 0) {
        $d = 255;
        $c--;
    }

    if ($c < 0) {
        $c = 255;
        $b--;
    }

    if ($b < 0) {
        $b = 255;
        $a--;
    }

    if ($a < 0) {
        die("IPv4 Range Exceeded");
    }

    return "$a.$b.$c.$d";
}

?>

To test both functions, you can write a for loop to generate approximately 16 million IP addresses back and forth, you can pipe the output to a file and store the results that way.

<?php

require 'function.ipinc.php';
require 'function.ipdec.php';

print("Increment:\n");
for ($i = 0, $ip = "100.0.0.0"; $i <= 16777215; $i++) {
    print("$ip\n");
    $ip = ipinc($ip);
}
print("----------\n");

print("Decrement:\n");
for ($i = 0, $ip = "100.255.255.255"; $i <= 16777215; $i++) {
    print("$ip\n");
    $ip = ipdec($ip);
}
print("----------\n");

die("Finished!\n");

?>

If you don't like assigning values through variable declarations, modify the functions so you can use pass by reference instead.

Hello fellows, How to create PHP script to show LAN ip address? This is very important when it comes to checking some connections using local area networking ip addresses. I am handling 20 computers and I am testing a website that can detect or may show LAN ip addresses. I want some suggestions from you. Regards, Kristen James.

The ISP provides a unique IP address to a home machine that is dialing via modem, and the IP address is unique for that session and it may change the next time for the machine. How to identify your server’s IP address: The $_SERVER is an array in PHP that contains the information regarding the headers, paths and script locations. The web

PHP can be used to get the IP Address of a website visitor, IP address of the server hosting the website and resolve the IP address to get the hostname a.k.a reverse DNS records. PHP has predefined $_SERVER array variable using which IP addresses of both visitors and host can be retrieved.

Aside from the obvious (localhost, 127.0.0.1) does PHP (command line interface!) have a mechanism for discovering the IP of the computer the script is running on? $_SERVER[*] will not work as this is not a Web app - this is a command line script.

Comments
  • Yes!!! Thanks!!!! it works and I think it is more efficient than the code I was thinhave been writing some code for this but since I was away form the computer I just used pen and paper. Will give it a ago and post it when I have mine. Your script works like a charm, thanks again.