?
Solved

Need PHP code for calculating subnets

Posted on 2008-11-13
6
Medium Priority
?
2,315 Views
Last Modified: 2012-05-05
Hi all,

I'm looking for a PHP script to calculate subnets.
User needs to provide IP address and subnetmask and press the Send-button.

The script must display the network adress, broadcast address and the hostrange for that IP-address...

Can anyone help pls?

Greetings
0
Comment
Question by:Pit76
  • 4
6 Comments
 
LVL 6

Expert Comment

by:matthewrhoades
ID: 22954293
0
 
LVL 9

Accepted Solution

by:
absx earned 2000 total points
ID: 22958019
Hi,

Previous poster probably already solved the problem better, but I felt intriqued to try this, results included as a snippet :)
<?
 
function zerofill($val,$len,$reverse = false){
	while(strlen($val)<$len)
		if($reverse)
			$val .= "0";
		else
			$val = "0".$val;
	return $val;
}
 
function hextoip($hex){
	for($i=0;$i<4;$i++)
		@$ip .= base_convert(substr($hex,$i*2,2),16,10).".";
	$ip = substr($ip,0,-1);
	return $ip;
}
 
function iptohex($ip){
	$iparr = explode(".",$ip);
	foreach($iparr as $i => $group){
		@$hex .= zerofill(base_convert($group,10,16),2);
	}
	return $hex;
}
 
function hextobin($hex){
	$bin = zerofill(base_convert($hex,16,2),32);
	return $bin;
}
 
function bintohex($bin) {
	$hex = zerofill(base_convert($bin,2,16),8);
	return $hex;
}
 
function getnet($ip, $netmask){
	$iphex = iptohex($ip);
	$netbin = hextobin($iphex);
	$maskhex = iptohex($netmask);
	$maskbin = hextobin($maskhex);
	
	$hostbits = substr($maskbin,strpos($maskbin,"0"));
	$netbits = substr($maskbin,0,strpos($maskbin,"0"));
	$nethex = bintohex(zerofill(substr($netbin,0,strlen($netbits)),32,true));
	
	$hosts = pow(2,strlen($hostbits));
	$available_hosts = $hosts-3;
	
	$net["ip"] = $ip;
	$net["mask"] = $netmask;
	$net["network"] = hextoip($nethex);
	$net["netstr"] = $net["network"]."/".strlen($netbits);
	$net["hosts"] = $available_hosts;
	$net["firstip"] = hextoip(zerofill(base_convert(base_convert($nethex,16,10)+1,10,16),8));
	$net["lastip"] = hextoip(zerofill(base_convert(base_convert($nethex,16,10)+$available_hosts+1,10,16),8));
	$net["broadcast"] = hextoip(zerofill(base_convert(base_convert($nethex,16,10)+$available_hosts+2,10,16),8));
	return $net;
}
 
?>
 
<form method="get">
<input type="text" maxlength="15" name="ip" value="<?=@$_GET["ip"]?>" /> IP or network address <br />
<input type="text" maxlength="15" name="mask" value="<?=@$_GET["mask"]?>" /> Subnet mask <br />
<input type="submit" value="Show network information" />
</form>
 
<?
if(!empty($_GET["ip"]) && !empty($_GET["mask"])){
	$net = getnet($_GET["ip"],$_GET["mask"]);
	echo "<pre>";
	print_r($net);
}
?>

Open in new window

0
 

Author Closing Comment

by:Pit76
ID: 31516546
thx. Can you explain to me how you separate the numbers with th"."?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 9

Expert Comment

by:absx
ID: 22973712
Um, I'm not sure if I get what you mean, but here's the basic idea:

The blocks of an IP address are converted to one hexadecimal number (192.168.0.1 becomes c0a80001). This hexadecimal number is used in calculating the start and end of the ranges and is later converted back to decimal form, two characters at a time, at which point dots are added to separate the blocks.

Has this anything to do with what you meant?
0
 
LVL 9

Expert Comment

by:absx
ID: 22973719
Also, line 48 should actually be fixed to

$available_hosts = $hosts-2;

to get the correct number of available hosts per network.
0
 
LVL 9

Expert Comment

by:absx
ID: 22973733
And making the previous change also requires changes to lines 56 and 57:

$net["lastip"] = hextoip(zerofill(base_convert(base_convert($nethex,16,10)+$available_hosts,10,16),8));
$net["broadcast"] = hextoip(zerofill(base_convert(base_convert($nethex,16,10)+$available_hosts+1,10,16),8));
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month13 days, 14 hours left to enroll

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question