Need PHP code for calculating subnets

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
Pit76Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

matthewrhoadesCommented:
0
absxCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Pit76Author Commented:
thx. Can you explain to me how you separate the numbers with th"."?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

absxCommented:
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
absxCommented:
Also, line 48 should actually be fixed to

$available_hosts = $hosts-2;

to get the correct number of available hosts per network.
0
absxCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.