Solved

# Need PHP code for calculating subnets

Posted on 2008-11-13
Medium Priority
2,315 Views
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.

Can anyone help pls?

Greetings
0
Question by:Pit76
• 4

LVL 6

Expert Comment

ID: 22954293
0

LVL 9

Accepted Solution

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;
}

\$iphex = iptohex(\$ip);
\$netbin = hextobin(\$iphex);

\$nethex = bintohex(zerofill(substr(\$netbin,0,strlen(\$netbits)),32,true));

\$hosts = pow(2,strlen(\$hostbits));
\$available_hosts = \$hosts-3;

\$net["ip"] = \$ip;
\$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));
return \$net;
}

?>

<form method="get">
<input type="text" maxlength="15" name="ip" value="<?=@\$_GET["ip"]?>" /> IP or network address <br />
<input type="submit" value="Show network information" />
</form>

<?
echo "<pre>";
print_r(\$net);
}
?>
``````
0

Author Closing Comment

ID: 31516546
thx. Can you explain to me how you separate the numbers with th"."?
0

LVL 9

Expert Comment

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

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

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));
0

## Featured Post

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