php中生成标准uuid(guid)的方法

来源:互联网 发布:mac 10.13 桌面日历 编辑:程序博客网 时间:2024/06/05 15:13
<?php
UUID是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的。通常平台会提供生成UUID的API。UUID按照开放软件基金会(OSF)制定的标准计算,用到了以太网卡地址、纳秒级时间、芯片ID码和许多可能的数字。由以下几部分的组合:当前日期和时间(UUID的第一个部分与时间有关,如果你在生成一个UUID之后,过几秒又生成一个UUID,则第一个部分不同,其余相同),时钟序列,全局唯一的IEEE机器识别号(如果有网卡,从网卡获得,没有网卡以其他方式获得),UUID的唯一缺陷在于生成的结果串会比较长。关于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)


function guid(){
    if(
function_exists('com_create_guid'
)){
        return
 com_create_guid
();
    }else{
        
mt_srand((double)microtime()*10000);
//optionalfor 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
();
?>



http://blog.sina.com.cn/s/blog_61dfab6b0100ucj5.html

原创粉丝点击