All solutions still have nothing to do with your application: Security !
You said that you will use these numbers as a product confirmation code (so you really want it to be unpredictable, otherwise it will be exploited.
None of the MySQL RANDOM built-in functions, nor any of the random functions that PHP provides today, are safe random functions. They behave pseudo-randomly, well, but they are all predictable!
You only have a chance to hack something of your own using /dev/urandom on a * nix machine or using the Crypto API on Windows. OpenSSL provides safe random numbers based on these mechanisms - you can reuse this either in the C extension for PHP, or by reading the output from the command line script called from PHP. See also this answer .
About your requirement for numbers to be consistent - is it really that important? This complicates the situation. Otherwise, it would be nice to go with a simple safe 6-byte random number encoded in a string using hexadecimal encoding (with a 12-character string). Although I would recommend doing it 10 bytes and 20 characters in order to be more secure.
But if you want to be consistent, which I interpret as monotonically increasing (since a simple +1 would be trivially predictable), this makes things a lot more complicated. And you donโt get anything from this complexity, the only thing that can happen is that you violate security by inventing an obscure scheme that is easy to use.
My suggestion: add another column that acts as a plain old automatically incrementing identifier and will add the code as a random number, constructed as a separate column as above. As far as I see, there is no need to require that the product activation code be an identifier at the same time.
emboss
source share