1

Hey guys, I am trying to find a way to generate a UID to be placed in a URL. Right now I am looking at ways to create a UID's without using a sequential index from a db. This is what I have come up with.

As far as I can tell this should only create duplicates if they are both created in the same 1/10000th of a second.

function uid()
{
    list($usec, $sec) = explode(" ", microtime());

    $prec = 5;
    $usec = round($usec, $prec); 
    $sec = round($sec - ($sec / 1.001), $prec);

    $time = ($sec + $usec);

    return base_convert($time, 10, 36);
}

Any ideas on why this function would be a bad idea?

Dreendle
  • 161
  • 4

3 Answers3

0

Why don't you use the real uid functionality of PHP? This should provide truly unique identifiers: http://php.net/manual/en/function.uniqid.php

As for a v4 compliant UUID, check this topic: PHP function to generate v4 UUID

Community
  • 1
  • 1
András Szepesházi
  • 6,483
  • 5
  • 45
  • 59
0

Well.. Maybe because autoincrement integers are mainly meant for this? Still there is a possibility of equal ids

kos
  • 478
  • 2
  • 10
0

Don't reinvent the wheel. Hash functions like MD5, SHA1, CRC32, etc. are best suited for this. Bear in mind that the shorter your hash key, the greater the likelihood of collisions.

bcosca
  • 17,371
  • 5
  • 40
  • 51