PHP 生成 Guid 唯一码

此文章发布于 61 个月前,部分信息可能已经过时,请自行斟酌确认。

在微软的技术平台上有 Guid 唯一码,号称能保证全球唯一,在 Windows 平台上 PHP 生成唯一码可以借助 com_dotnet 的com_create_guid()来实现。

PHP 生成 Guid 唯一码方法

确定 phpext 目录中已经下载好了 php_com_dotnet.dll,如果没有网上下载个,php7、php5.6 都已经内置了,只是没开启。
修改 php.ini 文件两个地址:

;extension=php_com_dotnet.dll 把前面的;注释号去掉
;com.allow_dcom = true 把前面;注释去掉,设置为 true

重启 IIS 运行 phpinfo()查看。

Linux 不支持 com 的兼容方式

function getGUID(){
    if (function_exists('com_create_guid')){
        return strtolower(trim(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;
    }
}

使用

$GUID = getGUID();
echo $GUID;
最后修改:2019 年 03 月 22 日 06 : 05 PM
如果觉得我的文章对你有用,请随意赞赏

发表评论