RE: [PHP] How to ask "if private IP"?
| From: | Andrés Robinet | Date: | Mon, 26 Nov 2007 04:00:30 +0000 |
| Subject: | RE: [PHP] How to ask "if private IP"? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-265013@lists.php.net to get a copy of this message | ||
$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