IP address range script
Moderator: General Moderators
- mhouldridge
- Forum Contributor
- Posts: 267
- Joined: Wed Jan 26, 2005 5:13 am
- mhouldridge
- Forum Contributor
- Posts: 267
- Joined: Wed Jan 26, 2005 5:13 am
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.
- mhouldridge
- Forum Contributor
- Posts: 267
- Joined: Wed Jan 26, 2005 5:13 am
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;
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>'
?>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_ipReal programmers don't comment their code. If it was hard to write, it should be hard to understand.
- mhouldridge
- Forum Contributor
- Posts: 267
- Joined: Wed Jan 26, 2005 5:13 am
- mhouldridge
- Forum Contributor
- Posts: 267
- Joined: Wed Jan 26, 2005 5:13 am
It's sorted now,
The following code works;
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>'