Jump to content

Recommended Posts

This works if say I do 192.168.0.1 - 192.168.1.254 it will work. However doing 192.168.0.1 - 192.168.1.10 will only generate up to .10 on both IP schemes. I see why, but I cannot figure out a way around this...

 

Thanks!

$start = explode('.', '192.168.10.1');
$end = explode('.', '192.168.11.10');

/**
 * Final output...
 */
$a = 0;
$b = 0;
$c = 0;
$d = 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++)
			{
				echo "{$a}.{$b}.{$c}.{$d}<br />";
			}
		}
	}
}

Link to comment
https://forums.phpfreaks.com/topic/216290-php-ip-range-generator/
Share on other sites

For some reason this creates an infinite loop when I try to increment $decimal using $decimal++, so I just incremented it long hand.

$startIP = '192.168.10.1';
$endIP = '192.168.11.10';

$startDecimal = sprintf("%u\n", ip2long($startIP));
$endDecimal   = sprintf("%u\n", ip2long($endIP));

for($decimal=$startDecimal; $decimal<=$endDecimal; $decimal=$decimal+1)
{
    echo long2ip($decimal) . "<br />\n";
}

I took the liberty to write out a script to perform the task stated by the OP... I don't have time to fin it atm but you can take a look and get an idea of how i'm doing it, possibly help you.. I'll try to finish it after my next class.

 

<?php
/////////////////////////////////////////
///^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^///
///^^ Calculate Ip Range difference ^^///
///^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^///
/////////////////////////////////////////
//Example (192.168.11.10) - (192.168.10.1) = 268 Addresses


////////////////////////////////
//### START AND END IP #s ###///
////////////////////////////////
$Start	= explode('.','192.168.10.1');
$End	= explode('.','192.168.11.10);
$Part	=;
////////////////////////////////////////////////////
//### Initiate Place holder for generated IP's ###//
////////////////////////////////////////////////////
$GendIP = Array();

///////////////////////////////
//### Min/Max IP # Ranges ###//
///////////////////////////////
$Max = 255;
$Min = 0;
$Cap = Array();
$NumIP = 0;

//////////////////////////
//### How Many IP's? ###//
//////////////////////////
for($i=0; $i<4; $i++)
{
//First check if the IP segments match
//as then there is not calc to be done
if($Start[$i] == $End[$i])
{
	//The first segment of the two IP's given match 
	//So there is nothing to be done.

	//Set cap place holder
	$Cap[$i] = nil;
}
else
{
	//The IP segments don't match so we will find a 			//range cap for this segment
	$Cap[$i] = $End[$i] - $Start[$i];
}

if($Cap[$i] != 'nil')
{
	$NumIP += $Cap[$i]
}
}



//Gen the Ip's
for($i=0; $i<=$NumIP; $i++)
{

}
?>

I took the liberty to write out a script to perform the task stated by the OP... I don't have time to fin it atm but you can take a look and get an idea of how i'm doing it, possibly help you.. I'll try to finish it after my next class.

 

I guess reading the other responses was too much work for you? I already provided a working solution that was more efficient than what you posted later.

I wouldn't use sprintf() at all:

 

as a person who never uses sprintf, I appreciate that! i wasn't about to spend time trying to figure out why we used it, but i'm glad we don't have to.

 

Heaven forbid you actually spend a few precious moments of your time to find out WHY I used it. You might actually learn something. I don't typically use it either. But, if you were to read the manual for ip2long() you would know that there is a valid reason to use sprintf() with the %u formatter!

 

From the manual (emphasis added)

Note:

Because PHP's integer type is signed, and many IP addresses will result in negative integers, you need to use the "%u" formatter of sprintf() or printf() to get the string representation of the unsigned IP address.

I wouldn't use sprintf() at all:

 

as a person who never uses sprintf, I appreciate that! i wasn't about to spend time trying to figure out why we used it, but i'm glad we don't have to.

 

Heaven forbid you actually spend a few precious moments of your time to find out WHY I used it. You might actually learn something. I don't typically use it either. But, if you were to read the manual for ip2long() you would know that there is a valid reason to use sprintf() with the %u formatter!

 

From the manual (emphasis added)

Note:

Because PHP's integer type is signed, and many IP addresses will result in negative integers, you need to use the "%u" formatter of sprintf() or printf() to get the string representation of the unsigned IP address.

 

Yes, but in this case we don't need a string representation of an unsigned integer.

I wouldn't use sprintf() at all:

 

as a person who never uses sprintf, I appreciate that! i wasn't about to spend time trying to figure out why we used it, but i'm glad we don't have to.

 

Heaven forbid you actually spend a few precious moments of your time to find out WHY I used it. You might actually learn something. I don't typically use it either. But, if you were to read the manual for ip2long() you would know that there is a valid reason to use sprintf() with the %u formatter!

 

From the manual (emphasis added)

Note:

Because PHP's integer type is signed, and many IP addresses will result in negative integers, you need to use the "%u" formatter of sprintf() or printf() to get the string representation of the unsigned IP address.

 

Yes, but in this case we don't need a string representation of an unsigned integer.

 

:rtfm:

 

The fact that sprintf returns a string is irrelevant. The fact is that you need the return value from ip2long as an unsigned integer in order to return the correct address when you pass it to long2ip.

 

u - the argument is treated as an            integer, and presented as an unsigned decimal number.

The fact that sprintf() returns a string is irrelevant.

No, the original problem was that using the post increment operator on the string was causing problems.  The fact is that in this case, an unsigned number string is not needed.

The fact is that you need the return value from ip2long() as an unsigned integer in order to return the correct address when you pass it to long2ip().

If possible, please post an example/test case of when using ip2long() and then long2ip() without using sprintf() to return a string representation of an unsigned number will give incorrect results.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.