Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Need assistance with PHP to compute start and end IP given base IP and subnet mask

Posted on 2013-11-16
8
Medium Priority
?
379 Views
Last Modified: 2013-11-18
Hi Team,

   just need some quick assistance with IP maninpulation in PHP such that given a base IP and a mask, I can compute the start IP of the range and the end IP.

 Example:Given two fields in the form:
IP_BASE = 192.168.88.0
MASK = 24

I need it to come up with
$loweripaddress = 192.168.88.1
$upperipaddress = 192.168.88.255

I have a partial snippet below but Lower_IP and Upper_IP have to be filled in by the person doing the form.   I am looking for a way that php can be able to complete this automatically. I also need them converted to decimal which i was able to figure out already.

  function beforeSave(&$record){
    $lowerdecimal =ip2long($record->val('Lower_IP'));
    $upperdecimal =ip2long($record->val('Upper_IP'));
    $record -> setValue('Lower_Dec', $lowerdecimal); 
    $record -> setValue('Upper_Dec', $upperdecimal);    
}

Open in new window


Thanks very much.
0
Comment
Question by:rleyba828
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39653870
Starting with this...

 Example:Given two fields in the form:
IP_BASE = 192.168.88.0
MASK = 24

I need it to come up with
$loweripaddress = 192.168.88.1
$upperipaddress = 192.168.88.255


I am unable to discern the value of MASK = 24.

Why does that get included in the question?
0
 

Author Comment

by:rleyba828
ID: 39653919
Hi Ray, basically MASK (actually a subnet mask) is one of the two fields the user has to type in the form, the other being the IP_Address.

In Networking terms, if you have an IP of 10.0.0.0/24,  (/24 is the mask), what that really translates to is 10.0.0.1 up to 10.0.0.255, where these two resulting values translate to the lower and upper IP ranges covered by the IP subnet range.

Other examples:

172.16.3.0 /24    --> 172.16.3.1, 172.16.3.255
10.0.5.0/30     --> 10.0.5.1, 10.0.5.2
192.168.0.0/16   --> 192.168.0.1, 192.168.255.255


So if the user types in 10.10.0.0   and 16 as the fields and clicks submit,   the php should come up with

$loweripaddress = 10.10.0.1
$upperipaddress = 10.10.255.255

which i will then use for further processing.

Thanks
0
 

Author Comment

by:rleyba828
ID: 39653925
eventually my code should look something like this.....just don't know how to setup the function.

  function beforeSave(&$record){
    $loweripaddress = somephpfunctiontogetlowerip ($BASE_IP, $MASK);
    $upperipaddress =anotherphpfunctiontogetupperrip ($BASE_IP, $MASK);
    $lowerdecimal =ip2long($loweripaddress);
    $upperdecimal =ip2long($upperipaddress);
    $record -> setValue('Lower_Dec', $lowerdecimal); 
    $record -> setValue('Upper_Dec', $upperdecimal);    
}

Open in new window

0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 35

Accepted Solution

by:
gr8gonzo earned 1400 total points
ID: 39653927
This is not my original function - most of it is from an old comment on the php.net site somewhere, but I'm not sure where.
 
<?php

print_r(iprange("192.168.1.0","24"));
// Array(
//     [0] => 192.168.1.0
//     [1] => 192.168.1.255
// )

function iprange($start,$cidrmask) {
$bits1=str_pad(decbin(ip2long($start)),32,"0",STR_PAD_LEFT);
$net=pow(2,(32-$cidrmask))-1;
$bits2=str_pad(decbin($net),32,"0",STR_PAD_LEFT);
for ($i=0;$i<32;$i++) {
if ($bits1[$i]==$bits2[$i]) $end.=$bits1[$i];
if ($bits1[$i]==1 and $bits2[$i]==0) $end.=$bits1[$i];
if ($bits1[$i]==0 and $bits2[$i]==1) $end.=$bits2[$i];
}
return array($start,long2ip(bindec($end)));
}
?>
0
 

Author Comment

by:rleyba828
ID: 39653933
Hi gr8gonzo,  yes, that is exactly what I need....let me adapt it to my code.....will let you know how it goes.
0
 

Author Comment

by:rleyba828
ID: 39654280
Hi Team,

  I've adapted the code above and my final (and working) code is now:

  function beforeSave(&$record){
    $auth =& Dataface_AuthenticationTool::getInstance();
    $user =& $auth->getLoggedInUser();
    $updatedate = date('Y-m-d H:i:s');
    $record->setValue('LAST_POLLED', $updatedate);
    $record->setValue('Record_Owner', $user->val('UserName'));
    $base_ip=($record->val('Base_IP'));
    $subnetmask=($record->val('Mask'));
    $lowerip=$base_ip;
    $bits1=str_pad(decbin(ip2long($base_ip)),32,"0",STR_PAD_LEFT);
    $net=pow(2,(32-$subnetmask))-1;
    $bits2=str_pad(decbin($net),32,"0",STR_PAD_LEFT);
    $end="";
    for ($i=0;$i<32;$i++) {
            if ($bits1[$i]==$bits2[$i]) $end.=$bits1[$i];
            if ($bits1[$i]==1 and $bits2[$i]==0) $end.=$bits1[$i];
            if ($bits1[$i]==0 and $bits2[$i]==1) $end.=$bits2[$i];
            }
    $upperip=long2ip(bindec($end));
    $record -> setValue('Lower_IP', $lowerip);
    $record -> setValue('Upper_IP', $upperip);
    $lowerdecimal =ip2long($record->val('Lower_IP'));
    $upperdecimal =ip2long($record->val('Upper_IP'));
    $record -> setValue('Lower_Dec', $lowerdecimal);
    $record -> setValue('Upper_Dec', $upperdecimal);
}

Open in new window


* The one small issue though is that the $lowerip should actually be $base_ip + 1.  I just don't know how to do this.
Example:  if $base_ip = 192.168.78.0, then $lowerip should be 192.168.78.1
If $base_ip = 10.2.3.0, $lowerip should be 10.2.3.1.

May I request final help in modifying the above code?

Thanks again.
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 600 total points
ID: 39654514
I can help with that part.  See http://www.laprbass.com/RAY_temp_rleyba828.php
Outputs: string(8) "10.2.3.1"

<?php // RAY_temp_rleyba828.php
error_reporting(E_ALL);
echo '<pre>';

// If $base_ip = 10.2.3.0, $lowerip should be 10.2.3.1.
// http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28296204.html#a39653933
// http://php.net/manual/en/function.ip2long.php

$base_ip = '10.2.3.0';
$int_ip  = ip2long($base_ip);
$int_ip++;
$new_ip  = long2ip($int_ip);

var_dump($new_ip);

Open in new window

Regards, ~Ray
0
 

Author Comment

by:rleyba828
ID: 39656161
Ah yes.....why didn't I think of that. Thanks ray and gr8gonzo for all the help.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

604 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