Forum Moderators: coopster
preg_match("/(192\.168\.(0-9){1,3}\.(0-9){1,3})¦(10\.(0-9){1,3}\.(0-9){1,3}\. (0-9){1,3})¦(172\.(16-31)\.(0-9){1,3}\.(0-9){1,3})/",$_SERVER['REMOTE_ADDR'])
[edited by: dreamcatcher at 3:09 pm (utc) on April 6, 2007]
[edit reason] Fixed slight side scroll. [/edit]
Here's what I'm testing with:
<?php
ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL);$addr = $_SERVER['REMOTE_ADDR'];
$regex = "/(192\.168\.(0-9){1,3}\.(0-9){1,3})¦(10\.(0-9){1,3}\.(0-9){1,3}\.(0-9){1,3})¦(172\.(16-31)\.(0-9){1,3}\.(0-9){1,3})¦(127\.0\.0\.1)/";echo "Remote: $addr (".preg_match($regex,$addr).")";
?>
I haven't had a chance to start playing with the numbers, and I added a localhost test.
(172\.[16-31]\.[0-9]{1,3}\.[0-9]{1,3})¦
You need to enclose all your [0-9] in brackets, but that range won't work, even in brackets.
Here's my test code:
<?php
ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL);$addr = Array ( "localhost" => $_SERVER['REMOTE_ADDR'], "test1" => "192.168.1.1", "test2" => "10.0.0.2", "test3" => "172.13.12.1" );
$regex = "/(192\.168\.[0-9]{1,3}\.[0-9]{1,3})¦(10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})¦(172\.[16-31]\.[0-9]{1,3}\.[0-9]{1,3})¦(127\.0\.0\.1)/";
while ( list($key, $value) = each ( $addr ) )
{
echo "Test $key: $value (".preg_match($regex,$value).")<br />";
}
?>
$regex = "/(192\.168\.[0-9]{1,3}\.[0-9]{1,3})¦(10\.[0-9]{1,3}\.[0-9]{1,3}\. [0-9]{1,3})¦(172\.[b](16¦17¦18¦19¦20¦21¦22¦23¦24¦25¦26¦27¦28¦29¦30¦31)[/b]
\.[0-9]{1,3}\.[0-9]{1,3})¦(127\.0\.0\.1)/x";
I hope someone will post a better way to do numeric ranges with regex. :)
Moderator's Note: I've added the 'x' pattern modifier [php.net] to the patten above so that I can fix the side scroll without affecting the pattern.
[edited by: eelixduppy at 7:59 pm (utc) on April 6, 2007]
[edit reason] fixed side scroll [/edit]
<?php
ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL);$addr = Array ( "localhost" => $_SERVER['REMOTE_ADDR'],
"test1" => "192.168.1.1", "test2" => "10.0.0.2",
"test3" => "172.16.12.1" ); // NOTE: This was 172.13.12.1 which would fail the test.// NOTE: I updated this:
$regex = "/(192\.168\.[0-9]{1,3}\.[0-9]{1,3})¦(10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})¦(172\.0?([6-9])¦([2][0-9])¦([3][0-1])\.[0-9]{1,3}\.[0-9]{1,3})¦(127\.0\.0\.1)/";while ( list($key, $value) = each ( $addr ) )
{
echo "Test $key: $value (".preg_match($regex,$value).")<br />";
}
?>
[edit]I updated it to something that will work[/edit]
[1][edited by: cmarshall at 6:14 pm (utc) on April 6, 2007]
regex = "/(192\.168\.[0-9]{1,3}\.[0-9]{1,3})¦(10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})¦ (172\.0?([red][b][1][/b][/red][6-9])¦([2][0-9])¦([3][0-1])\.[0-9]{1,3}\.[0-9]{1,3})¦ (127\.0\.0\.1)/x" [edited by: eelixduppy at 8:43 pm (utc) on April 6, 2007]
[edit reason] fixed side scroll [/edit]
I needed to disable codes.
Here we are, "raw":
<?php
ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL);
$addr = Array ( "localhost" => $_SERVER['REMOTE_ADDR'],
"test1" => "192.168.1.1",
"test2" => "10.0.0.2",
"test3" => "172.31.12.1" );
$regex = "/(192\.168\.[0-9]{1,3}\.[0-9]{1,3})";
$regex .= "¦(10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})";
$regex .= "¦(172\.0?([1][6-9])¦([2][0-9])¦([3][0-1])\.[0-9]{1,3}\.[0-9]{1,3})";
$regex .= "¦(127\.0\.0\.1)/";
while ( list($key, $value) = each ( $addr ) )
{
echo "Test $key: $value ";
echo "(".preg_match($regex,$value)."[smilestopper])<br />";
}
?>
ip2long():
$ip = ip2long($_SERVER['REMOTE_ADDR']);
$islocal = false;
if($ip >= ip2long('192.168.0.0') && $ip <= ip2long('192.168.255.255'))
$islocal = true;
elseif($ip >= ip2long('10.0.0.0') && $ip <= ip2long('10.255.255.255'))
$islocal = true;
elseif($ip >= ip2long('172.16.0.0') && $ip <= ip2long('172.31.255.255'))
$islocal = true;
elseif($ip == ip2long('127.0.0.1'))
$islocal = true;
Or use the function described here: [php.net...]