On Mon, 18 Mar 2002, Charles Williams wrote:
>> Ordinarily I would suggest CIDR subnets but the sample IP addresses you
>> provided do not comply - they seem to be random numbers. Is this because
>> you need to deal with arbitrary sets of IP addresses rather than valid
>> subnets, or because you were just in a hurry while typing your message to
>> the list?
>> Also, I wouldn't recommend using the slash to identify ranges of IP
>> addresses, because the slash already has a specific meaning in this
>> context: the number after it specifies the size of the subnet mask. For
>> instance, to refer to the range, you'd write
> Sorry.  forgot about the cisco standard thing there when I typed it all up.
> hehe.  been a long day.
> Anyway.  the IP addresses provided were just arbitrary addresses to show the
> format to be used.
> of course the /xx (cisco method) will be used and the IP-IP format will be
> used for a range as well.

Well, it's still not clear to me whether you meant to allow arbitrary 
sets of IP addresses or only valid subnets...

Assuming the latter, it's really easy. You can always describe an IP4
subnet with two numbers: a network number from 0 to 4294967295 (just treat
the subnet address like one great big hex number), and a mask size from 0
to 32. Two columns, presto, you're done with the database stuff, and you
can do all sorts of clever searches.

If, on the other hand, you want people to be able to store things like
",," then you have two
choices, depending on how you're going to be searching.

If you never need to search the stored space by IP address (i.e., if 
you're always going to be looking up by customer number or something), 
then I'd recommend parsing it and verifying into an array in PHP and then 
just serializing it into a string and storing that. You can pull it back 
out into a set of start/end pairs which is easy to work with given PHP's 
flexible arrays.

In any case, to parse, first set some ground rules for the users: They
must separate items by commas, for instance. Explode on comma, then for
each element, strip and look for a slash or a dash. If you get a slash,
validate and process it as a subnet. If you get a dash, validate and
process it as an arbitrary range. Validation of an IP address and subnet 
mask size is trivial and I'll leave that as homework.

If you do need to search by IP address, then you're probably going to want 
to make a separate table just to hold an arbitrary number of starting and 
ending points.

table ip_chunk:
  customer_id number int not null
  start_ip int
  end_ip int

Then you can do things like "select customer.name from customer, ip_chunk 
where customer.customer_id=ip_chunk.customer_id and 
{$ip}>=ip_chunk.start_ip and {$ip}<=ip_chunk.end_ip group by 


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

Reply via email to