Dev, Nice work! Thanks for implementing the book's algorithm/proving it in PHP-PCRE, and I do like your extension into an internal/external function.
I've not implemented your code (bookmarked for later) but some initial comments: - don't forget some of the other 'funnies' in the IP addressing scheme, most notably 127.0.0.1 = localhost (which is also an 'internal' address) - I'd need to read/remind myself of others... - in the $parts[0]>=255 type of construct, remember that the > is superfluous - or the RegEx is overly specific (hence the discussion point earlier) - also the $parts[0] is a string, so "255" will be (marginally) faster - similarly you could make $ip_state="0"; into a boolean expression (and function return value) which might enable the function call to read more smoothly - regardless, $ip_state is currently sometimes treated as a string and once as an integer. - I don't know your IP allocations but in net_check() shouldn't all the if() logical ops be ANDs? Any thoughts of extending it to work with IPng addresses (or whatever they're calling it these days)? Great stuff, keep it coming! (and when you've finished perhaps you could submit it to one of the script libraries/improve on what they might already have?) =dn > Well after reading John'ss and DL Neil's replys i came up with 2 functions. > The first is to check to see if the entry is a valid IP address and the > Next is to determine if it is a Private Network IP or not!. > > You can see this in action at: > http://www.my-tangled-web.net/codebank/code/ip_check.php > and the sorce for that page is at: > http://www.my-tangled-web.net/codebank/getsource.php?dir=&file=ip_check.php > > Please if you test it and find a problem let me know! > > Thanks > > > function ipcheck($ip_chk){ > $parts=explode(".",$ip_chk); > if (preg_match > ("/^([01]?\d\d?|2[0-4]\d|25[0-4])\.([01]?\d\d?|2[0-4]\d|25[0-4])\.([01]?\d\d ?|2[0-4]\d|25[0-4])\.([01]?\d\d?|2[0-4]\d|25[0-4])$/", > $ip_chk)) > { > if($parts[0]==0 || $parts[0]>=255 ||$parts[1]>=255 || > $parts[2]>=255 || $parts[3]>=255){ > $ip_state="0"; > } > elseif($parts[0]<255 && $parts[1]<255 && $parts[2]<255 && > $parts[3]<255){ > $ip_state="1"; > } > } > else { > $ip_state=0; > } > $this="that"; > return $ip_state; > } > > function net_check($ip_2_chk){ > $parts=explode(".",$ip_2_chk); > if($parts[0]==10){ > $network="internal"; > } > elseif($parts[0]==172 && $parts[1]>=16 || $parts[1]<=31){ > $network="internal"; > } > elseif($parts[0]==192 && $parts[1]==168){ > $network="internal"; > } > else { > $network="external"; > } > return $network; > }; > > At 07:47 PM 12/4/2002 +0000, DL Neil wrote: > >John, > > > > > I think it'd be hard to verify the range with a regex. ip2long and long2ip > > > do not validate. So, an option would be to write your own little function > > > that splits the $ip on the period, verifies there are 4 parts, then checks > > > that each part is between 1 and 255 inclusive. > > > > > >My other post on the subject notwithstanding, I agree. > > > >When I get time/when I get a round tuit, I'd like to do a performance test > >on the (short) IP RegEx against what you have outlined: > > > >explode against "." > >run resultant array through 0-255 validation function > >- and still have the semantic issues of 0.0.0.0, etc... > > > >Meantime, will sit here whistling and quietly muttering into my beard (and > >dreaming of tuits), > >=dn > > > > > >-- > >PHP General Mailing List (http://www.php.net/) > >To unsubscribe, visit: http://www.php.net/unsub.php > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php