
July 5th, 2008, 08:05 PM
|
|
|
Function to tell if IP address is in a range
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
|

July 6th, 2008, 11:55 AM
|
|
|
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.
|

July 6th, 2008, 12:15 PM
|
|
|
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 );
|

July 6th, 2008, 04:35 PM
|
|
|
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
|

July 6th, 2008, 04:45 PM
|
|
|
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!
|

July 6th, 2008, 08:15 PM
|
|
|
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
==================
|

July 6th, 2008, 11:05 PM
|
|
|
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
|

July 9th, 2008, 04:55 AM
|
|
|
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
|

July 13th, 2008, 12:55 PM
|
|
|
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.
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|