UUID基本概念与组成原理
UUID是一种确保在同一时空范围内所有设备具备唯一性的数字标识。
多数平台均提供生成UUID的API接口。该标准遵循开放软件基金会(OSF)规范,计算过程中会结合以太网卡物理地址、纳秒级时间戳、芯片ID码以及多种随机数值。
其构成要素包括:当前日期与时间(首段反映时间变化,若间隔数秒再生成,则首段不同而其余部分一致)、时钟序列,以及全局唯一的IEEE机器识别码(优先从网卡获取,若无网卡则通过其他途径获取)。UUID的主要局限性在于生成的字符串长度较长。
在该标准中,应用最为广泛的形态是微软定义的GUID(Globals Unique Identifiers)。
例如在ColdFusion中,可通过CreateUUID()函数便捷地生成UUID,其呈现格式为xxxxxxxx-xxxx-xxxx- xxxxxxxxxxxxxxxx(即8-4-4-16结构),其中每个x代表0-9或a-f区间内的一个十六进制字符。
相比之下,标准的UUID格式定义为:xxxxxxxx-xxxx-xxxx-xxxxxx-xxxxxxxxxx(即8-4-4-4-12结构)。
<?php
function guid(){
if (function_exists('com_create_guid')){
return com_create_guid();
}else{
mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
$charid = strtoupper(md5(uniqid(rand(), true)));
$hyphen = chr(45);// "-"
$uuid = chr(123)// "{"
.substr($charid, 0, 8).$hyphen
.substr($charid, 8, 4).$hyphen
.substr($charid,12, 4).$hyphen
.substr($charid,16, 4).$hyphen
.substr($charid,20,12)
.chr(125);// "}"
return $uuid;
}
}
echo guid();
?>
上述内容整合了PHP中生成标准UUID(GUID)的详细方法,旨在提供帮助。如有任何疑问欢迎留言,小编将及时回复。同时也感谢大家对本站网站的支持!