问题
I want to give our users in the database a unique alpha-numeric id. I am using the code below, will this always generate a unique id? Below is the updated version of the code:
old php:
// Generate Guid
function NewGuid() {
$s = strtoupper(md5(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();
echo $Guid;
echo "<br><br><br>";
New PHP:
// Generate Guid
function NewGuid() {
$s = strtoupper(uniqid("something",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();
echo $Guid;
echo "<br><br><br>";
Will the second (new php) code guarantee 100% uniqueness.
Final code:
PHP
// 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();
echo $Guid;
$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;
}
echo base_encode($Guid, $alphabet);
}
So for more stronger uniqueness, i am using the $Guid as the key generator. That should be ok right?
回答1:
If you want the id to be unique, you should generate a GUID using uniqid.
Your method will not guarantee uniqueness at all. And will likely create collisions.
回答2:
The code below will generate you a unique alpha-numeric ID from a number. I took it from Flickr groups. Two functions to encode and decode:
$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;
}
UPDATE
Usage:
echo base_encode(123456789, $alphabet); //should output: bUKpk
回答3:
uniqid: <?php $substr = substr(md5(rand()),0,22);echo " $substr ";?>
回答4:
If it's truly random, then you can not guarantee uniqueness (unless you keep generating random keys and checking them against your database until you find a new one, which is a horrible idea).
Does it really need to be random?
Why not just use an autoincrement column in the table, and then convert it to base-62 (or so) if you want to reference it as an alphanumeric value?
来源:https://stackoverflow.com/questions/4514168/unique-alpha-numeric-generator