ElA*_*ecs 1 php uuid guid token
我需要创建一个令牌/密钥以将其用作数字顺序,因此这应该是唯一的,令牌必须类似于"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;
}
Run Code Online (Sandbox Code Playgroud)
关于你究竟想要什么,问题不是很明确.如果要随机生成这样的字符串,可以执行以下操作:
$length = 20;
$characters = ‘0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ’;
$random_string = "";
for ($p = 0; $p < $length; $p++) {
$random_string .= $characters[mt_rand(0, strlen($characters))];
}
Run Code Online (Sandbox Code Playgroud)
要确保新生成的令牌是唯一的,您必须跟踪所有先前生成的令牌并执行检查.