mercredi 2 octobre 2019

create unique 8 digit number

I am creating an application at the moment that needs to give the user a unique 8 digit code (numbers only) that they can enter to access restricted parts of my website, think of it as a rudimentary auth, i.e enter the passcode, the passcode matches a user, the user is returned. What I am having trouble with is creating a unique 8 digit code. I know PHP has uniqid() but that returns a string that is too long.

I have thought about creating a hash of time() and the users IP address, but laravel's Hash::create returns integers and chars.

Can anyone point in the direction of creating a random 8 digit code? I have also tried mt_rand but obvioulsy there is the chance that this could create matching digits, unless of course I check the database for the code before saving and re-generate if it exists?




Aucun commentaire:

Enregistrer un commentaire