Solved

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

Posted on 2013-11-16
8
372 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 110

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
Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

 
LVL 35

Accepted Solution

by:
gr8gonzo earned 350 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 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 150 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
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.

691 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