Question

I have a client's IP address and an array full of simple words. Let's say...

$ip_addr = ip2long('172.16.254.12'); //returns an integer

and

$words = array('cat', 'dog', 'nest', 'barn', 'etc'); //arbitrary length

I'm trying to figure out a clever way to map the ip address to a position in the array consistently so 172.16.254.12 always turns into cat etc. and that another IP would turn into say, dog. Ideally this process would not be easily reversible so as to obscure the client's IP address and there should not be too many collisions (but collisions not so important). I know this needs a hash table, but not entirely sure how to implement it. Don't need to worry about ipv6 for right now.

Was it helpful?

Solution

No hashing function required:

$ip_addr = ip2long('172.16.254.12'); //returns an integer
$words = array('cat', 'dog', 'nest', 'barn', 'etc'); //arbitrary length

$chosen_word = $words[$ip_addr % count($words)];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top