Solved

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

Posted on 2013-11-16
8
359 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
  • 5
  • 2
8 Comments
 
LVL 108

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
 
LVL 34

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php date - compare 4 40
Echo images using file system 2 32
Form Processing in PHP 11 32
PHP & MySQL - Rounding Results from a Select Query 3 13
If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 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…

920 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

16 Experts available now in Live!

Get 1:1 Help Now