Forum Moderators: coopster

Message Too Old, No Replies

Regex - Matching Local IPs

Another quickie...

         

ahmedtheking

1:25 pm on Apr 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm just trying to get PHP to sniff out local users using preg_match; can anyone spot my error?

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]

joelgreen

1:39 pm on Apr 6, 2007 (gmt 0)

10+ Year Member



maybe change (0-9) to [0-9]

also i'm note sure if (16-31) would work

cmarshall

1:41 pm on Apr 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I see no error.

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.

ahmedtheking

2:34 pm on Apr 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry I shouldn't have said 'error'; more like it's not working! I tried [] but their for classes really.

cmarshall

3:25 pm on Apr 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can you give me some IP numbers to test?

ahmedtheking

3:32 pm on Apr 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



192.168.1.1
10.0.0.2
172.13.12.1

They're all local IPs!

cmarshall

3:54 pm on Apr 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I gotta run, so I can't spend more time on this, but here's your problem:

(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 />";
}
?>

whoisgregg

5:29 pm on Apr 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To accomplish the numeric range, you can specify each possible member of the range:

$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]

cmarshall

5:46 pm on Apr 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This will work. You gave a test number outside the range, which complicated it a bit. This is awkward, but not crazy awkward:

<?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]

cmarshall

6:11 pm on Apr 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I fixed a problem. It now works (I'm pretty sure).

cmarshall

8:40 pm on Apr 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Duh. Not quite. There was a cut and paste error, so what I posted was not what I had in my test script:

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]

cmarshall

2:24 am on Apr 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member


ARGH! No! It wasn't a cut-and-paste error!

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 />";
}
?>

ahmedtheking

7:16 pm on Apr 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Woah lotsa stuff! Thanks to everyone for getting back! I'm going to digest this and get back to you!

borntobeweb

4:41 am on Apr 9, 2007 (gmt 0)

10+ Year Member



Instead of regex, you can also use
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...]