Connecting Tech Pros Worldwide Forums | Help | Site Map

Function to tell if IP address is in a range

axlq
Guest
 
Posts: n/a
#1: Jul 5 '08

After failing to find this in my searches, I thought I'd ask
here: Is there a php function that can tell me if an IP address
falls within a range?

Something like this:

$result = is_in_ip_range($_SERVER['REMOTE_ADDR'], '78.157.128.0/19');

I need this for denying access to my web contact form from certain IP
ranges.

I can write my own, but wondered if there's something in the massive
php function library that I overlooked.

-A

C. (http://symcbean.blogspot.com/)
Guest
 
Posts: n/a
#2: Jul 6 '08

re: Function to tell if IP address is in a range


On Jul 5, 7:40 pm, a...@spamcop.net (axlq) wrote:
Quote:
After failing to find this in my searches, I thought I'd ask
here: Is there a php function that can tell me if an IP address
falls within a range?
>
Something like this:
>
$result = is_in_ip_range($_SERVER['REMOTE_ADDR'], '78.157.128.0/19');
>
I need this for denying access to my web contact form from certain IP
ranges.
>
I can write my own, but wondered if there's something in the massive
php function library that I overlooked.
>
-A
There's a PEAR class - Net_IPv4. There are probably other
implementations too - try phpclasses, freshmeat or google.

C.
Betikci Boris
Guest
 
Posts: n/a
#3: Jul 6 '08

re: Function to tell if IP address is in a range


On Jul 5, 9:40 pm, a...@spamcop.net (axlq) wrote:
Quote:
After failing to find this in my searches, I thought I'd ask
here: Is there a php function that can tell me if an IP address
falls within a range?
>
Something like this:
>
$result = is_in_ip_range($_SERVER['REMOTE_ADDR'], '78.157.128.0/19');
>
I need this for denying access to my web contact form from certain IP
ranges.
>
I can write my own, but wondered if there's something in the massive
php function library that I overlooked.
>
-A
You need to use string manipulation functions to process second
parameter, however your
Function parameters should be like this:
ip_func($_SERVER['REMOTE_ADDR'],<IP TO START>,<IP TO STOP>);

Ex. ip_func($_SERVER['REMOTE_ADDR'] , 78.157.128.0 , 78.157.128.255 );
axlq
Guest
 
Posts: n/a
#4: Jul 6 '08

re: Function to tell if IP address is in a range


In article <3ccab4d5-05b3-4502-ac8b-693543188b81@z72g2000hsb.googlegroups.com>,
Betikci Boris <pardust@gmail.comwrote:
Quote:
>On Jul 5, 9:40 pm, a...@spamcop.net (axlq) wrote:
Quote:
>After failing to find this in my searches, I thought I'd ask
>here: Is there a php function that can tell me if an IP address
>falls within a range?
Quote:
>Function parameters should be like this:
>ip_func($_SERVER['REMOTE_ADDR'],<IP TO START>,<IP TO STOP>);
>
>Ex. ip_func($_SERVER['REMOTE_ADDR'] , 78.157.128.0 , 78.157.128.255 );
Oh, that's right. If I passed the second two arguments as strings,
I could simply use lexical comparison to determine if the first
argument is between them.

Thanks.
-A
Anonymous
Guest
 
Posts: n/a
#5: Jul 6 '08

re: Function to tell if IP address is in a range


axlq wrote:
Quote:
>
After failing to find this in my searches, I thought I'd ask
here: Is there a php function that can tell me if an IP address
falls within a range?
>
Something like this:
>
$result = is_in_ip_range($_SERVER['REMOTE_ADDR'], '78.157.128.0/19');
Make the second parameter two parameters then the check if $IP is
between $fromIP and $toIP is very easy.

function is_in_ip_range($IP, $fromIP, $toIP)

{
if( (ip2long($fromIP) < ip2long($IP)) && (ip2long($IP) < ip2long($toIP))
)
{return TRUE}
else
{return FALSE}
}


Bye!
Jerry Stuckle
Guest
 
Posts: n/a
#6: Jul 6 '08

re: Function to tell if IP address is in a range


axlq wrote:
Quote:
In article <3ccab4d5-05b3-4502-ac8b-693543188b81@z72g2000hsb.googlegroups.com>,
Betikci Boris <pardust@gmail.comwrote:
Quote:
>On Jul 5, 9:40 pm, a...@spamcop.net (axlq) wrote:
Quote:
>>After failing to find this in my searches, I thought I'd ask
>>here: Is there a php function that can tell me if an IP address
>>falls within a range?
>
Quote:
>Function parameters should be like this:
>ip_func($_SERVER['REMOTE_ADDR'],<IP TO START>,<IP TO STOP>);
>>
>Ex. ip_func($_SERVER['REMOTE_ADDR'] , 78.157.128.0 , 78.157.128.255 );
>
Oh, that's right. If I passed the second two arguments as strings,
I could simply use lexical comparison to determine if the first
argument is between them.
>
Thanks.
-A
>
Is 101.01.01.01 within the range of 98.98.98.98 and 110.110.110.110?
Passed as a string, it is not.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Paul Lautman
Guest
 
Posts: n/a
#7: Jul 6 '08

re: Function to tell if IP address is in a range


Jerry Stuckle wrote:
Quote:
axlq wrote:
Quote:
>In article
><3ccab4d5-05b3-4502-ac8b-693543188b81@z72g2000hsb.googlegroups.com>,
>Betikci Boris <pardust@gmail.comwrote:
Quote:
>>On Jul 5, 9:40 pm, a...@spamcop.net (axlq) wrote:
>>>After failing to find this in my searches, I thought I'd ask
>>>here: Is there a php function that can tell me if an IP address
>>>falls within a range?
>>
Quote:
>>Function parameters should be like this:
>>ip_func($_SERVER['REMOTE_ADDR'],<IP TO START>,<IP TO STOP>);
>>>
>>Ex. ip_func($_SERVER['REMOTE_ADDR'] , 78.157.128.0 , 78.157.128.255
>>);
>>
>Oh, that's right. If I passed the second two arguments as strings,
>I could simply use lexical comparison to determine if the first
>argument is between them.
>>
>Thanks.
>-A
>>
>
Is 101.01.01.01 within the range of 98.98.98.98 and 110.110.110.110?
Passed as a string, it is not.
In the context of IP ranges it is not, but more of a problem is:

78.157.128.2 would appear to be in the range 78.157.128.18 to 78.157.128.22


axlq
Guest
 
Posts: n/a
#8: Jul 9 '08

re: Function to tell if IP address is in a range


In article <4870E738.2F90CDEA@nowhere.invalid>,
Anonymous <anonymous@nowhere.invalidwrote:
Quote:
>axlq wrote:
Quote:
>>
>After failing to find this in my searches, I thought I'd ask
>here: Is there a php function that can tell me if an IP address
>falls within a range?
>>
>Something like this:
>>
>$result = is_in_ip_range($_SERVER['REMOTE_ADDR'], '78.157.128.0/19');
>
>Make the second parameter two parameters then the check if $IP is
>between $fromIP and $toIP is very easy.
>
>function is_in_ip_range($IP, $fromIP, $toIP)
>
>{
>if( (ip2long($fromIP) < ip2long($IP)) && (ip2long($IP) < ip2long($toIP))
>)
{return TRUE}
>else
{return FALSE}
>}
ip2long - THAT's what I was looking for! Thanks!

I can even do masking with that, for ranges specified like my 2nd
argument above.

-Alex
C. (http://symcbean.blogspot.com/)
Guest
 
Posts: n/a
#9: Jul 13 '08

re: Function to tell if IP address is in a range


On Jul 6, 4:34 pm, a...@spamcop.net (axlq) wrote:
Quote:
In article <3ccab4d5-05b3-4502-ac8b-693543188...@z72g2000hsb.googlegroups.com>,
Betikci Boris <pard...@gmail.comwrote:
>
Quote:
On Jul 5, 9:40 pm, a...@spamcop.net (axlq) wrote:
Quote:
After failing to find this in my searches, I thought I'd ask
here: Is there a php function that can tell me if an IP address
falls within a range?
Function parameters should be like this:
ip_func($_SERVER['REMOTE_ADDR'],<IP TO START>,<IP TO STOP>);
>
Quote:
Ex. ip_func($_SERVER['REMOTE_ADDR'] , 78.157.128.0 , 78.157.128.255 );
>
Oh, that's right. If I passed the second two arguments as strings,
I could simply use lexical comparison to determine if the first
argument is between them.
>
Thanks.
-A
No - that's only going to work for 8/16/32 bit subnets.

C.
Closed Thread


Similar PHP bytes