I am using the first half code below to generate a unique ID. Then i am using the entry generated in the first half as a variable for the second piece of code that generates another unique ID that is shorter, will the code i am using guarantee 100% uniqueness. I am new so please bear with me:
PHP:
// 1st Piece of code
// Generate Guid
function NewGuid() {
$s = strtoupper(uniqid(rand(),true));
$guidText =
substr($s,0,8) . '-' .
substr($s,8,4) . '-' .
substr($s,12,4). '-' .
substr($s,16,4). '-' .
substr($s,20);
return $guidText;
}
// End Generate Guid
$Guid = NewGuid();
//2nd Piece of code
$alphabet = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
function base_encode($num, $alphabet) {
$base_count = strlen($alphabet);
$encoded = '';
while ($num >= $base_count) {
$div = $num/$base_count;
$mod = ($num-($base_count*intval($div)));
$encoded = $alphabet[$mod] . $encoded;
$num = intval($div);
}
if ($num) $encoded = $alphabet[$num] . $encoded;
return $encoded;
}
function base_decode($num, $alphabet) {
$decoded = 0;
$multi = 1;
while (strlen($num) > 0) {
$digit = $num[strlen($num)-1];
$decoded += $multi * strpos($alphabet, $digit);
$multi = $multi * strlen($alphabet);
$num = substr($num, 0, -1);
}
return $decoded;
}
ob_start();
echo base_encode($Guid, $alphabet); //should output: bUKpk
$ider = ob_get_contents();
ob_end_clean();
Will the code i am using guarantee 100% uniqueness?
Of course it's not possible to 100% guarantee uniqueness with a random GUID. It's very likely that each GUID will be unique, but there is only a finite (but very large) number of GUIDs. If you generate enough of them you will eventually run out of unused GUIDs and be forced to generate a duplicate. And the birthday paradox means that randomly choosing GUIDs will on average give you a duplicate sooner than you might think.
But it's probably not worth worrying about in practice as even with the birthday paradox, it is still extremely unlikely to happen.