function ip_validate ($ip) {
$ips = explode(".", $ip);
$result = false;
if ( count($ips) == 4 ) {
$result = true;
foreach( $ips as $key => $val ){
if ( strlen($val) != 3 or is_numeric($val) == false ) {
$result = false;
}
}
}
return $result;
}
function ipv4Breakout2 ($ip_address, $ip_nmask) {
//convert ip addresses to long form
$ip_address_long = ip2long($ip_address);
$ip_nmask_long = ip2long($ip_nmask);
//caculate network address
$ip_net = $ip_address_long & $ip_nmask_long;
//caculate first usable address
$ip_host_first = ((~$ip_nmask_long) & $ip_address_long);
$ip_first = ($ip_address_long ^ $ip_host_first) + 1;
//caculate last usable address
$ip_broadcast_invert = ~$ip_nmask_long;
$ip_last = ($ip_address_long | $ip_broadcast_invert) - 1;
//caculate broadcast address
$ip_broadcast = $ip_address_long | $ip_broadcast_invert;
//Output
$ip_net_short = long2ip($ip_net);
$ip_first_short = long2ip($ip_first);
$ip_last_short = long2ip($ip_last);
$ip_broadcast_short = long2ip($ip_broadcast);
$block_info = array(array("network" => $ip_net_short),
array("first_host" => $ip_first_short),
array("last_host" => $ip_last_short),
array("broadcast" => $ip_broadcast_short),
$hosts);
//print_r($block_info);
return $block_info;
}
//print_r($_POST);
if ( $_POST["calc"] == "Calculate" ) {
$ip_address = trim($_POST["ipa"]);
$ip_nmask = trim($_POST["sbn"]);
if ( ip_validate($ip_address) == true and ip_validate($ip_nmask) == true ) {
$subnets = ipv4Breakout2 ($ip_address, $ip_nmask);
echo "[Results]
";
echo "Network:".$subnets[0]["network"]."
";
echo "First Host:".$subnets[1]["first_host"]."
";
echo "Last Host:".$subnets[2]["last_host"]."
";
echo "Broadcast:".$subnets[3]["broadcast"]."
";
} else {
echo "[Input Error]
";
}
if ( ip_validate($ip_address) == false ) {
echo "IP Address is invalid.
";
}
if ( ip_validate($ip_nmask) == false ) {
echo "Subnet Mask is invalid.
";
}
}
/**
include("./class/SubnetCalculator.php");
$subnet = new SubnetCalculator("173.164.246.201",29);
$report = $subnet->getSubnetArrayReport();
print_r($report);
**/
?>