in reply to Quickly determine which IP range an IP belongs to.

  1. Convert the addresses and masks into 32-bit numbers or 4-byte strings.
  2. For each range,
    1. "And" ("&") the specified IP with the range's mask
    2. If the result is the start IP of the masks's range, the address is in that range.

If you have lots of ranges, you could group them by mask.

$user_lookup{inet_aton($mask)}{inet_aton($start_addr)} = $user_num;