Let MySQL do all the work:
An IP address is simply a 32-bit number. It can be converted to it's numerical equivalent a little like this:
my @octets=split(/\./,$ip_address);
my $numerical_address=($octets[3]+(255*$octets[2])+(65535*$octets[1])+
+(16777216*$octets[0]));
Store this in your database as a 32-bit integer, which is much more eficient than an 18-byte character field for IP address.
Then, to see if another IP address is in the same class C, do something like this:
SELECT count(*) from table WHERE (int(ip/256)*256) = (int($ip_address_
+to_be_compared_to/256)*256)
Any result greater than 0 is a hit.
Note that the figure of 256 is for a class C subnet.
It can be changed to suit any subnet mask you want to use.
Edit: Oops, that should be 256, not 255.....
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.