In order to generate a unique token to relate certain information in SQL, after reviewing some documentation, I see the options to generate a token that is unique. This token will be present in two tables and in each of the two tables in multiple rows. I see two options, for a 32 character token.
Option 1
$ttime = microtime(true)*1000;
$token = $ttime . str_shuffle("ABCDEFGHIJKLNMOPQR");
Option 2
$token = bin2hex(random_bytes(16));
My doubt comes from reading that it is not advisable to use the microtime() function to generate unique tokens.
An alternative would be to use
uniqueid()
, get a prefixed unique identifier based on the current time in microseconds.Example A:
Manual: uniqid
Now if you want to use it
openssl_random_pseudo_bytes
to generate cryptographically secure tokens, it generates a pseudo-random byte string, with the number of bytes determined by the length parameter .Example B:
Manual: openssl_random_pseudo_bytes
Edit: Now that I have a little more time, I'll leave you with another example that I have saved a long time ago from SO , which I quite like.
For example, if you want to encrypt an id and store this encryption in the DB and later decrypt it to work with this ID , you could for example create a function like the example.
Example C:
Maybe these will help you:
EXAMPLE:
I hope I've helped!
I use this:
I don't use
microtime
, but hey, it's an option. BesisI have a personal one in which I have it in a function and it can generate the number of characters according to the number that it sends, it allows it to generate uppercase and lowercase characters and with numbers