如何在php上创建一个唯一的20个单字节令牌/密钥/ ID?

时间:2010-03-04 07:54:14

标签: php guid token uuid

我需要创建一个令牌/密钥以将其用作数字顺序,因此这应该是唯一的,令牌必须类似于“6X990742MG185953R”,因此我们可以像条形码一样使用它,例如。 http://barcodes4.me/barcode/c128b/6X990742MG185953R.png

我们不能使用UUID或GUID因为很长,我们就越接近这个:

function uuid64() {
 $uuid = uuid(); // some UUID v4
 $byteString = "";
 $uuid = str_replace("-", "", $uuid);
 for($i = 0; $i < strlen($uuid); $i += 2) {
  $s = substr($uuid, $i, 2);
  $d = hexdec($s);
  $c = chr($d);
  $byteString = $byteString.$c;
 } 

 $b64uuid = base64_encode($byteString);
 // Replace the "/" and "+" since they are reserved characters
 $b64uuid = str_replace("/", "_", $b64uuid);
 $b64uuid = str_replace("+", "-", $b64uuid);
 // Remove the trailing "=="
 $b64uuid = substr($b64uuid, 0, strlen($b64uuid) - 2); 

 return $b64uuid;
}

function uuid64() { $uuid = uuid(); // some UUID v4 $byteString = ""; $uuid = str_replace("-", "", $uuid); for($i = 0; $i < strlen($uuid); $i += 2) { $s = substr($uuid, $i, 2); $d = hexdec($s); $c = chr($d); $byteString = $byteString.$c; } $b64uuid = base64_encode($byteString); // Replace the "/" and "+" since they are reserved characters $b64uuid = str_replace("/", "_", $b64uuid); $b64uuid = str_replace("+", "-", $b64uuid); // Remove the trailing "==" $b64uuid = substr($b64uuid, 0, strlen($b64uuid) - 2); return $b64uuid; }

4 个答案:

答案 0 :(得分:5)

关于你究竟想要什么,问题不是很清楚。如果要随机生成这样的字符串,可以执行以下操作:

$length = 20;
$characters = ‘0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ’;
$random_string = "";    
for ($p = 0; $p < $length; $p++) {
    $random_string .= $characters[mt_rand(0, strlen($characters))];
}

为确保新生成的令牌是唯一的,您必须跟踪所有先前生成的令牌并执行检查。

答案 1 :(得分:3)

我认为您在上一条评论中回答了这个问题,是的,直接从您的数据库自动增量ID +填充字符生成UID,例如

function uid($n) {
    $id_part = base_convert($n, 10, 36);
    $rand_part = str_shuffle('abcdefghijklmnopqrstuvwxyz0123456789');
    return sprintf("%05s%.15s", $id_part, $rand_part);
}

这会创建一个UID,其中包含5个chars base-36主id + 15个垃圾字符

答案 2 :(得分:0)

我找到了这个函数,返回一个类似tinyurl风格的标记

function gen_uuid($len = 8)
{
    $hex = md5("some_salt_please" . uniqid("", true));
    $pack = pack('H*', $hex);
    $uid = base64_encode($pack);
    $uid = ereg_replace("[^A-Z0-9]", "", strtoupper($uid));
    if ($len<4)
        $len=4;
    if ($len>128)
        $len=128;
    while (strlen($uid)<$len)
        $uid = $uid . gen_uuid(22);
    return substr($uid, 0, $len);
}

function gen_uuid($len = 8) { $hex = md5("some_salt_please" . uniqid("", true)); $pack = pack('H*', $hex); $uid = base64_encode($pack); $uid = ereg_replace("[^A-Z0-9]", "", strtoupper($uid)); if ($len<4) $len=4; if ($len>128) $len=128; while (strlen($uid)<$len) $uid = $uid . gen_uuid(22); return substr($uid, 0, $len); }

答案 3 :(得分:0)

如果你想要一个简短的唯一ID,你肯定需要一个数据库来保存所有以前的ID。

while (true)
{
    $prefix = "";
    $mid = $prefix . rand(100000,999999) . chr(rand(65, 90));
    $check = mysql_query("SELECT id FROM ids WHERE id = '$mid'");
    if (mysql_num_rows($check) != 0)
    {
        //duplicate
    }
    else
    {
        mysql_query("insert into ids VALUES('$mid')");
        break 1;
    }
}