How to ask "if private IP"?

This is a discussion on How to ask "if private IP"? within the PHP General forums, part of the PHP Programming Forums category; I use $aa=$_SERVER["REMOTE_ADDR"]; and if(($aa=="192.168.2.108") || ($aa=="192.168....


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-26-2007
Ronald Wiplinger
 
Posts: n/a
Default How to ask "if private IP"?

I use $aa=$_SERVER["REMOTE_ADDR"];

and

if(($aa=="192.168.2.108") || ($aa=="192.168.2.34")) {
$aa="61.64.101.101"; // for testing put in a public IP
}


However, I would like to cover all private IPs (192.168.x.x and 10.x.x.x
and 172.??.x.x). How can I do that simple?

bye

Ronald

Reply With Quote
  #2 (permalink)  
Old 11-26-2007
dterrors@hotmail.com
 
Posts: n/a
Default Re: How to ask "if private IP"?

On Nov 25, 7:51 pm, ron...@elmit.com (Ronald Wiplinger) wrote:
> I use $aa=$_SERVER["REMOTE_ADDR"];
>
> and
>
> if(($aa=="192.168.2.108") || ($aa=="192.168.2.34")) {
> $aa="61.64.101.101"; // for testing put in a public IP
>
> }
>
> However, I would like to cover all private IPs (192.168.x.x and 10.x.x.x
> and 172.??.x.x). How can I do that simple?
>
> bye
>
> Ronald


Unless I'm misunderstanding you, you want:

if (preg_match('/^(192\.168|10\.)/', $_SERVER["REMOTE_ADDR"]))

If I'm wrong, then I'll at least point you in the right direction-
look into preg_match and regular expressions. (And maybe look into
perl).





Reply With Quote
  #3 (permalink)  
Old 11-26-2007
dterrors@hotmail.com
 
Posts: n/a
Default Re: How to ask "if private IP"?

On Nov 25, 8:00 pm, dterr...@hotmail.com wrote:
> On Nov 25, 7:51 pm, ron...@elmit.com (Ronald Wiplinger) wrote:
>
> > I use $aa=$_SERVER["REMOTE_ADDR"];

>
> > and

>
> > if(($aa=="192.168.2.108") || ($aa=="192.168.2.34")) {
> > $aa="61.64.101.101"; // for testing put in a public IP

>
> > }

>
> > However, I would like to cover all private IPs (192.168.x.x and 10.x.x.x
> > and 172.??.x.x). How can I do that simple?

>
> > bye

>
> > Ronald

>
> Unless I'm misunderstanding you, you want:
>
> if (preg_match('/^(192\.168|10\.)/', $_SERVER["REMOTE_ADDR"]))
>
> If I'm wrong, then I'll at least point you in the right direction-
> look into preg_match and regular expressions. (And maybe look into
> perl).


er:

if (preg_match('/^(192\.168|10\.|172\.)/', $_SERVER["REMOTE_ADDR"]))


Reply With Quote
  #4 (permalink)  
Old 11-26-2007
ZeldorBlat
 
Posts: n/a
Default Re: How to ask "if private IP"?

On Nov 25, 8:03 pm, dterr...@hotmail.com wrote:
> On Nov 25, 8:00 pm, dterr...@hotmail.com wrote:
>
>
>
> > On Nov 25, 7:51 pm, ron...@elmit.com (Ronald Wiplinger) wrote:

>
> > > I use $aa=$_SERVER["REMOTE_ADDR"];

>
> > > and

>
> > > if(($aa=="192.168.2.108") || ($aa=="192.168.2.34")) {
> > > $aa="61.64.101.101"; // for testing put in a public IP

>
> > > }

>
> > > However, I would like to cover all private IPs (192.168.x.x and 10.x.x.x
> > > and 172.??.x.x). How can I do that simple?

>
> > > bye

>
> > > Ronald

>
> > Unless I'm misunderstanding you, you want:

>
> > if (preg_match('/^(192\.168|10\.)/', $_SERVER["REMOTE_ADDR"]))

>
> > If I'm wrong, then I'll at least point you in the right direction-
> > look into preg_match and regular expressions. (And maybe look into
> > perl).

>
> er:
>
> if (preg_match('/^(192\.168|10\.|172\.)/', $_SERVER["REMOTE_ADDR"]))


Close, but not all of 172.* is private. According to RFC 1918, the
following are reserved for private use:

10.0.0.0 - 10.255.255.255 (10/8)
172.16.0.0 - 172.31.255.255 (172.16/12)
192.168.0.0 - 192.168.255.255 (192.168/16)

There are some other ones, too. 169.254/16, 169.254.0/24,
169.254.255/24 are "link-local" addresses, while 127.0.0.0/8 is
reserved for the loopback interface.
Reply With Quote
  #5 (permalink)  
Old 11-26-2007
Andrés Robinet
 
Posts: n/a
Default RE: [PHP] How to ask "if private IP"?

$remoteIP = $_SERVER['REMOTE_ADDR'];
$parts = explode('.', $remoteIP); // Get IP bytes in an array
$isPrivateIP = false;

/* single class A */
$classA = $parts[0] == 10;
/* 16 contiguous class Bs */
$classB = $parts[0] == 172 && $parts[1] >= 16 && $parts[1] <= 31;
/* 256 contiguous class Cs */
$classC = $parts[0] == 192 && $parts[1] == 168;
If ($classA || $classB || $classC || $remoteIP == '127.0.0.1') {
$isPrivateIP = true;
}

I would include the three tests inline (class A, B, C) inside the "if"
condition to get some performance benefit for short circuit boolean
evaluation (meaning that once one condition is true, the parser will stop
checking the others).

Rob

Andrés Robinet | Lead Developer | BESTPLACE CORPORATION
5100 Bayview Drive 206, Royal Lauderdale Landings, Fort Lauderdale, FL 33308
| TEL 954-607-4207*| FAX 954-337-2695 |
Email: info@bestplace.net *| MSN Chat: best@bestplace.net *| *SKYPE:
bestplace | *Web: bestplace.biz* | Web: seo-diy.com

> -----Original Message-----
> From: Ronald Wiplinger [mailto:ronald@elmit.com]
> Sent: Sunday, November 25, 2007 9:52 PM
> To: PHP General list
> Subject: [php] How to ask "if private IP"?
>
> I use $aa=$_SERVER["REMOTE_ADDR"];
>
> and
>
> if(($aa=="192.168.2.108") || ($aa=="192.168.2.34")) {
> $aa="61.64.101.101"; // for testing put in a public IP
> }
>
>
> However, I would like to cover all private IPs (192.168.x.x and
> 10.x.x.x
> and 172.??.x.x). How can I do that simple?
>
> bye
>
> Ronald

Reply With Quote
  #6 (permalink)  
Old 11-26-2007
mike
 
Posts: n/a
Default Re: [PHP] How to ask "if private IP"?

i did this once

$ip = sprintf("%u",intval(ip2long($_SERVER['REMOTE_ADDR'])));

and then did checks to see if it was between the ranges for 10.0.0.0,
192.168 etc...

apparently i lost that code. but it was pretty simple.
http://www.faqs.org/rfcs/rfc1918.html

10.0.0.0 - 10.255.255.255 (10/8 prefix)
172.16.0.0 - 172.31.255.255 (172.16/12 prefix)
192.168.0.0 - 192.168.255.255 (192.168/16 prefix)

and then what, 224.0.0.0-255.255.255.255? and not 0.0.0.0.

i had it all figured out... just print out what the 10.0.0.0 is in
integer, and 10.255.255.255 and such, and then just check if the
integer is between any of the "invalid" ranges. the integer math
should be very fast.
Reply With Quote
  #7 (permalink)  
Old 11-26-2007
Jochem Maas
 
Posts: n/a
Default Re: [PHP] How to ask "if private IP"?

Ronald Wiplinger wrote:
> I use $aa=$_SERVER["REMOTE_ADDR"];
>
> and
>
> if(($aa=="192.168.2.108") || ($aa=="192.168.2.34")) {
> $aa="61.64.101.101"; // for testing put in a public IP
> }
>
>
> However, I would like to cover all private IPs (192.168.x.x and 10.x.x.x
> and 172.??.x.x). How can I do that simple?


probably not the best or fastest way to do it but here is what I use:

function isPrivateIP($theip)
{
foreach (array("10.0.0.0/8",
"172.16.0.0/12",
"192.168.0.0/16") as $subnet)
{
list($net, $mask) = explode('/', $subnet);
if(isIPInSubnet($theip, $net, $mask))
return true;
}

return false;
}

function isIPInSubnet($ip, $net, $mask)
{
$firstpart = substr(str_pad(decbin(ip2long($net)), 32, "0", STR_PAD_LEFT) ,0 , $mask);
$firstip = substr(str_pad(decbin(ip2long($ip)), 32, "0", STR_PAD_LEFT), 0, $mask);

return (strcmp($firstpart, $firstip) == 0);
}


>
> bye
>
> Ronald
>

Reply With Quote
  #8 (permalink)  
Old 11-26-2007
Jochem Maas
 
Posts: n/a
Default Re: [PHP] How to ask "if private IP"?

Ronald, I really dont care if my email doesn't reach you, making normal people jump
through hoops because you want to avoid spam is not the right way to do things,
next time I'll remember not to answer your questions as your not going to
['be able to'] read my answers:

<BLA BLA BLA>
This message was created automatically by mail delivery software (TMDA).

Your message attached below is being held because the address
<jochem@iamjochem.com> has not been verified.

To release your message for delivery, please send an empty message
to the following address, or use your mailer's "Reply" feature.

ronald-confirm-1196077490.5142.da0db9@elmit.com

This confirmation verifies that your message is legitimate and not
junk-mail. You should only have to confirm your address once.

If you do not respond to this confirmation request within 14 days,
your message will not be delivered.
</BLA BLA BLA>



Jochem Maas wrote:
> Ronald Wiplinger wrote:
>> I use $aa=$_SERVER["REMOTE_ADDR"];
>>

Reply With Quote
  #9 (permalink)  
Old 11-26-2007
William Betts
 
Posts: n/a
Default Re: [PHP] How to ask "if private IP"?

Below is a quick example. This isn't the best way to do it, just
another way. I personally would convert them to integers then compare
instead of doing it the way I'm doing it below.

<?php

function privateIP($ip)
{
if ( (($ip >= "10.0.0.0") && ($ip <= "10.255.255.255")) ||
(($ip >= "192.168.0.0") && ($ip <= "192.168.255.255")) ||
(($ip >= "172.16.0.0") && ($ip <= "172.31.255.255")) ) {
return true;
}
return false;
}

if (privateIP("192.168.1.1")) {
print "hmm";
}
?>

William Betts
http://www.phpbakery.com

On Nov 26, 2007 6:08 AM, Jochem Maas <jochem@iamjochem.com> wrote:
> Ronald, I really dont care if my email doesn't reach you, making normal people jump
> through hoops because you want to avoid spam is not the right way to do things,
> next time I'll remember not to answer your questions as your not going to
> ['be able to'] read my answers:
>
> <BLA BLA BLA>
> This message was created automatically by mail delivery software (TMDA).
>
> Your message attached below is being held because the address
> <jochem@iamjochem.com> has not been verified.
>
> To release your message for delivery, please send an empty message
> to the following address, or use your mailer's "Reply" feature.
>
> ronald-confirm-1196077490.5142.da0db9@elmit.com
>
> This confirmation verifies that your message is legitimate and not
> junk-mail. You should only have to confirm your address once.
>
> If you do not respond to this confirmation request within 14 days,
> your message will not be delivered.
> </BLA BLA BLA>
>
>
>
> Jochem Maas wrote:
> > Ronald Wiplinger wrote:
> >> I use $aa=$_SERVER["REMOTE_ADDR"];
> >>

>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Reply With Quote
  #10 (permalink)  
Old 11-27-2007
Philip Thompson
 
Posts: n/a
Default Re: [PHP] How to ask "if private IP"?

On Nov 26, 2007 6:08 AM, Jochem Maas <jochem@iamjochem.com> wrote:

> Ronald, I really dont care if my email doesn't reach you, making normal
> people jump
> through hoops because you want to avoid spam is not the right way to do
> things,
> next time I'll remember not to answer your questions as your not going to
> ['be able to'] read my answers:



I agree... except this can be avoided if you only reply to the list. No need
to reply to the sender AND the list. If the sender is on the list.... s/he
will get the email......

Ok. Sorry for my lil morning rant. ;-)

Cheers,
~Philip

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 09:29 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0