I'm working on a yellow pages site that has around 1 million entries. I'm currently using random md5 hash as id but think its a bad idea (being longer, consumes more space).
Is there any nicer way of generating an unpredictable, random, short alphanumeric id in PHP other than using uniqid()
function. I'm not using auto-increment ids, base64 etc since data scraping would be very easy.
You not only want them to be unpredictable, but also unique.
Here's a technique: Randomly come up with two very large prime numbers. Call them p and q. q must be larger than p. Keep these numbers secret, because you'll be using them as your key generator from here on.
Each time you want to generate a new identifier:
n
= (number_of_rows) + 1;id
= p^(n) MOD qMost large number libraries have a
powermod
method, so it would beIf, for my primes, I picked
Then
Even though it appears random, you're guaranteed to get no repetitions until you reach q
Of course, if someone figures out what your two initial prime numbers are, they would be able to guess the next number from the previous, so you would have to keep them secret. But I don't believe there's a way to work out what the source numbers are (short of just trying each possible pair of prime numbers, which of cause would take a prohibitively long time).
Here's a page on .NET fiddle to demonstrate this: https://dotnetfiddle.net/oUkbvy