vendredi 29 mars 2019

Generating a URL friendly, unguessable, Unique ID in PHP or MySQL

I'm trying to create a unique id for a MySQL database table that can be used in a URL


It should be

  • URL friendly - preferably alphanumeric
  • Unguessable - it should be difficult to randomly find another ID by manipulating the value in the URL
  • Unique - very unlikely to collide with an existing number

I toyed with using UUID() or UUID_SHORT() from MySQL but while these are unique, other values may be guessable from a known ID

The same could be said for PHP's uniqid() function


I'm currently looking at generating a 33 character hex string using

uniqid(bin2hex(random_bytes(10))) 

But I'm wondering if that is more or less likely to be unique than a random 33 character hex string using

dechex(random_int(0,15)).bin2hex(random_bytes(16))




Aucun commentaire:

Enregistrer un commentaire