Hi all,
I’m fairly new to the world of PHP & MySQL and have decided to kickoff my first project by writing a simple application that manages IP address assignments (I work for a small ISP who currently use Excel to manage this which is an absolute nightmare to use across multiple users).
I’m about 30% through completion but have hit a brick wall and wanted some advice on how best to tackle my problem.
Basically I have a admin page where users can go to add new networks into the app. The page is very basic and asks for two values:
Example:
Network: 192.168.1.0
Netmask: 255.255.255.0
From this I can work out the network, netmask, broadcast & range IP’s:
$ip_addr = “192.168.1.0”;
$subnet_mask = “255.255.255.0”;
$ip = ip2long($ip_addr);
$nm = ip2long($subnet_mask);
$nw = ($ip & $nm);
$bc = $nw | (~$nm);
echo "IP Address: " . long2ip($ip) . "
";
echo "Subnet Mask: " . long2ip($nm) . "
";
echo "Network Address: " . long2ip($nw) . "
";
echo "Broadcast Address: " . long2ip($bc) . "
";
echo "Host Range: " . long2ip($nw + 1) . " -> " . long2ip($bc - 1) . "
";
Now from the two values I spoke about earlier (Network & Netmask) I’d like to get PHP to add all the hosts for this range (including network & broadcast IP’s) into a MySQL table called ip_table (one IP per row).
i.e.
192.168.1.0
192.168.1.1
192.168.1.2
…and so on
I figured some form of while loop would be required so I tried the following:
$nw = long2ip($nw);
$bc = long2ip($bc);
while ($nw <= $bc)
{
echo "$nw ";
++$nw;
}
This seemed to work but only actually runs printing 192.168.1.0, 192.168.1.1 & 192.168.1.2 (think the multiple decimals maybe causing this unexpected behaviour).
If anyone has any advice at all it would be much appreciated.
Thanks for your time
Regards
Mark