IP address range script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

Should I be outputting $found_ip instead?


thanks,
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Yep. That's what the for loop is for, generating an IP and seeing if it's assigned or not. If it isn't it's stored in $found_ip.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

yes - I see now...

er... When I try to output the found_ip i get the following error;

Notice: Undefined variable: found_ip in F:\Auditwebsite\IP2.php on line 81


Im not sure why it is saying this, as I have define found_IP as $found_ip = ('192.168.1.'.$counter);
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Hmm. That error is displayed likely because you are referencing $found_ip outside of it's scope (the for loop). However, I've never had a problem with that before. Try declaring $found_ip = FALSE; right before the if(!$result) line.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

Bit lost now (have been from the start to be honest)

Here is the last par tof my code, please could you shed some light on it;

Code: Select all

while($row = mysql_fetch_assoc($result)){
							$ips[] = $row['IP'];
							} 
							
							for	($counter = 0; 	
							$counter >= 255; 															
							++$counter){
							$found_ip = 0;
							if(!in_array('194.131.241.'.$counter,$ips))							
								{
								$found_ip = ('194.131.241.'.$counter);								
																
							}}
							
								echo'<pre>';	
								print_r($found_ip);
								echo '</pre>'																								
							
							?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Code: Select all

//assuming $result gets all ips already stored in the db,
//this loop goes through the result set and puts all the
//ips in the $ips array
while($row = mysql_fetch_assoc($result))
{
  $ips[] = $row['IP'];
} 

//this for loop loops through each possible ip 
//in the 194.131.241.X range

//$counter needs to start at 1.  You had it at 0, 
//and the original code I gave had it at 'i' (WRONG!)
$found_ip = 0; //moved this declaration outside the for loop
for ($counter = 1; $counter >= 255; ++$counter)
{
  //for each dynamically created ip, check if it exists
  //in the $ips array.
  if(!in_array('194.131.241.'.$counter,$ips))                            
  {
    //if it doesn't, then the current IP is free
    //to use
    $found_ip = ('194.131.241.'.$counter);
    break;//no sense continuing if we've found what we need                                
   }
}
                                                                                               
//$found_ip is a string, so we call echo, rather than print_r
echo $found_ip
Does this clear things up?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

Yeah, that's good, clears everything up....

With this new code my result is simply zero. I can see that the array has the listed taken IPs, so the query is working and pulling them into the $ips variable.

hmmm....

Your help so far has been fantastic, thanks again.
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

It's sorted now,

The following code works;

Code: Select all

while($row = mysql_fetch_assoc($result)){
							$ips[] = $row['IP'];
							} 
							
						$found_ips=ARRAY();
						$available_ips=ARRAY();
						for($counter = 1; $counter <= 255; $counter++){  
						 	if(in_array('192.168.241.'.$counter,$ips))  {  
							  $found_ips[] = '192.168.241.'.$counter;  }  
						else  {    $available_ips[] = '194.131.241.'.$counter;  }
						}
							
							echo'<pre>';
							print_r($available_ips); 
							echo '</pre>'
Post Reply