CodeIgniter Forums
IP Range Generation - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: IP Range Generation (/thread-35118.html)



IP Range Generation - El Forum - 10-19-2010

[eluser]echoDreamz[/eluser]
Code:
$start = explode('.', $_POST['ipRange1']);
    $end = explode('.', $_POST['ipRange2']);

    /**
     * Final output...
     */
    $a = 0;
    $b = 0;
    $c = 0;
    $d = 0;
    $total = 0;
    
    // First segment
    for ($a = $start[0]; $a < 1 + $end[0]; $a++)
    {
        // Second segment
        for ($b = $start[1]; $b < 1 + $end[1]; $b++)
        {
            // Third segment
            for ($c = $start[2]; $c < 1 + $end[2]; $c++)
            {
                // Fourth segment
                for ($d = $start[3]; $d < 1 + $end[3]; $d++)
                {
                    $total++;
                    echo "{$a}.{$b}.{$c}.{$d}<br />";
                }
            }
        }
    }

The above works fine for ranges like 192.168.1.1 - 192.168.2.254 however for items like 192.168.1.10 - 192.168.2.15 it only lists the 6. I see why it is doing this, I just cant find a way around this.

Thank for the help!


IP Range Generation - El Forum - 10-19-2010

[eluser]danmontgomery[/eluser]
Code:
function test() {
    $ip1 = ip2long('192.168.1.10');
    $ip2 = ip2long('192.168.2.15');
        
    while($ip1 <= $ip2) {
        echo long2ip($ip1++).'<br/>';
    }
}



IP Range Generation - El Forum - 10-19-2010

[eluser]echoDreamz[/eluser]
Yes, it hit me about about 10 minutes ago lol.