RE: How to ask "if private IP"?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



$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@xxxxxxxxxxxxx  | MSN Chat: best@xxxxxxxxxxxxx  |  SKYPE:
bestplace |  Web: bestplace.biz  | Web: seo-diy.com

> -----Original Message-----
> From: Ronald Wiplinger [mailto:ronald@xxxxxxxxx]
> Sent: Sunday, November 25, 2007 9:52 PM
> To: PHP General list
> Subject:  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

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



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux