Need PHP code for calculating subnets

Posted on 2008-11-13
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?

Question by:Pit76
    LVL 6

    Expert Comment

    LVL 9

    Accepted Solution


    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){
    			$val .= "0";
    			$val = "0".$val;
    	return $val;
    function hextoip($hex){
    		@$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" />
    if(!empty($_GET["ip"]) && !empty($_GET["mask"])){
    	$net = getnet($_GET["ip"],$_GET["mask"]);
    	echo "<pre>";

    Open in new window


    Author Closing Comment

    thx. Can you explain to me how you separate the numbers with th"."?
    LVL 9

    Expert Comment

    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 ( 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?
    LVL 9

    Expert Comment

    Also, line 48 should actually be fixed to

    $available_hosts = $hosts-2;

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

    Expert Comment

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

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now