[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
Solved

# Matching IP number with preset networks

Posted on 2002-06-10
Medium Priority
246 Views
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
Question by:Vahan Yerkanian
[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
• 3
• 2

LVL 32

Expert Comment

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

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

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

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

Hope this helps a bit.

0

LVL 1

Author Comment

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

LVL 40

Accepted Solution

Richard Quadling earned 600 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

ID: 7066709

Thanks Richard!
0

## Featured Post

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients. Â This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for â€¦
This article discusses four methods for overlaying images in a container on a web page
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.
###### Suggested Courses
Course of the Month12 days, 17 hours left to enroll