Solved

Matching IP number with preset networks

Posted on 2002-06-10
6
210 Views
Last Modified: 2006-11-17
What I need is to check if the given IP number is
from one of the networks supplied in an array like
$a[]=array('195.250.64.0', '0.0.31.255');
$a[]=array('212.73.64.0', '0.0.31.255');
$a[]=array('212.42.192.0', '0.0.31.255');
etc, where the second field is netmask. the function
should return true or false.

Anyone ever coded such thing?
0
Comment
Question by:vahan
  • 3
  • 2
6 Comments
 
LVL 32

Expert Comment

by:Batalf
ID: 7066438
What kind of netmask is that? is it opposite of what's normal. Normally "1" determines host/server, when "0" determines client addresses.
 
0
 
LVL 1

Author Comment

by:vahan
ID: 7066453
It's a reverse netmask. The problem is that I'm not so good with the netmask calculation, that's why I posted the question :0 The netmasks in above mean that each 31.255 should cover 32 C class networks.
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 7066522
This is off the top of my head, but why not turn the IP address into a 32 bit integer and the mask into the range.

So.

195.250.64.0 = (((((195 * 256) + 250) * 256) + 64) * 256) + 0 = 3287957504

And the mask of 0.0.31.255, is 31 * 256 + 255 = 8191.

So, you can now check to see if the address is between 3287957504 and 3287965695.

You can split the IP address into bits by using

$a = preg_split("(\d{1,3}).(\d{1,3}).(\d{1,3}).(\d{1,3})",$IP_address);

And then you can use $a[0] .. $a[3].

Hope this helps a bit.

Richard Quadling.
0
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.

 
LVL 1

Author Comment

by:vahan
ID: 7066575
Can u post a working code here? 150 points is not enough?
0
 
LVL 40

Accepted Solution

by:
RQuadling earned 150 total points
ID: 7066610
Hopefully this will show you what I mean.

<?php

$aValidIPs['212.73.64.0'] = '0.0.31.255';
$aValidIPs['212.42.192.0'] = '0.0.31.255';
$aValidIPs['195.250.64.0'] = '0.0.31.255';

$YourIPtoTest = '195.250.92.24';

function GetInt($sGI)
{
echo "DEBUG : Checking $sGI<br>";
$a = explode('.',$sGI,4);
echo "DEBUG : Array is $a[0] - $a[1] - $a[2] - $a[3]<br>";
echo "DEBUG : Value is " . (((((($a[0] * 256) + $a[1]) * 256) + $a[2]) * 256) + $a[3]) . "<br>";
return (((((($a[0] * 256) + $a[1]) * 256) + $a[2]) * 256) + $a[3]);
}

function CheckIP($aCIP,$IP)
{
foreach($aCIP as $BaseIP => $Range)
 {
 $FirstIPVal = GetInt($BaseIP);
 $LastIPVal = $FirstIPVal + GetInt($Range);
 $IPVal = GetInt($IP);
 echo "DEBUG : FirstIP $FirstIPVal - LastIP $LastIPVal - IP $IPVal<br>";
 if (($IPVal >= $FirstIPVal) and ($IPVal <= $LastIPVal))
  {
  return true;
  }
 }
return false;
}

if (CheckIP($aValidIPs,$YourIPtoTest) == true)
 {
 echo 'Valid';
 }
else
 {
 echo 'Invalid';
 }

?>

Regards,

Richard.
0
 
LVL 1

Author Comment

by:vahan
ID: 7066709
"Works as advertised" :0

Thanks Richard!
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
help with PHP array/lists 3 38
PHP7 and Sql Server Windows 2008 R2 13 81
What is the best PDF generator to use? 1 23
Scope of $_SESSION 17 13
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

930 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

14 Experts available now in Live!

Get 1:1 Help Now