I did some tests to find out how long id would use different hash functions. I also used convBase to convert from 0-9a-z to 0-9a-zA-Z as suggested :
$i = mt_rand(1, PHP_INT_MAX); sha1($i): 40 322c73c44958e4219fd6679aead094192cb672fe convBase(sha1($i)): 34 1JHrlXObHSVMcbn2bHRBCBIRD3RVKQHMQzg md5($i): 32 7b09f8cd76be44403b90e971a5a61e6c convBase(md5($i)): 28 5bgdGpBZekbb3PQlILrSKMtHC24A $i: 9 107300785 convBase($i): 5 7gdPP
So, sha1 is very long, even if it is converted to another base. Also, I'm still not sure if MD5 is safe. Anyway, with 28 characters, it's still pretty long if you want to post it to twitter url: http://mydomainsux.com/invite/5bgdGpBZekbb3PQlILrSKMtHC24A
The shortest solution with convBase($i)
has a non-uniqueness problem. You will need to check this manually. However, I think this is the way to go, because if you use a hash, there can also be a natural conspiracy, so you still need to check.
I read that you can pre-create an identifier in a table and just select once when you need it. Thus, you do not need to check uniqueness every time.
source share