28

致力于在 php 文件中生成 guid。我正在使用com_create_guid(). 它在本地主机上工作正常,但我在远程服务器中显示以下错误。

致命错误:在第 6 行调用未定义函数 com_create_guid()

我的代码是(仅引导部分)

$guid = com_create_guid();
 echo $guid;

有任何想法吗

4

4 回答 4

47

您可以手动创建 GUID:

function getGUID(){
    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;
    }
}

用法:

$GUID = getGUID();
echo $GUID;

跟随:

http://guid.us/GUID/PHP

http://php.net/manual/en/function.com-create-guid.php

于 2013-08-13T10:43:16.533 回答
10

要扩展上面部分正确的答案:

取自http://php.net/manual/en/com.installation.php

“从 PHP 5.4.5 开始,COM 和 DOTNET 不再内置在 php 核心中。您必须在 php.ini 中添加 COM 支持”

扩展=php_com_dotnet.dll

于 2013-10-30T12:42:42.003 回答
2

您必须运行低于 5 的 PHP 版本,否则您必须在 LINUX 机器上运行,因为 COM 是基于 Windows 的扩展。

试试这个脚本并确保。

echo function_exists('com_create_guid')
    ? "Yes" +  com_create_guid()
    : "Nope !"
;
于 2013-08-13T10:55:50.753 回答
2

可能的根本原因

没有加载 php_com_dotnet.dll 的 Windows 系统(检查您的 php.ini 文件)以及非 Windows 系统将无法使用com_create_guid()

解决方案

我组装和修改了以下代码,作为我自己的一些想法和更改的高潮(例如始终支持大括号),以及来自多个来源的大量建议,用于实现支持支撑和非支撑 UID 创建的跨平台和跨 PHP 版本功能. 在函数调用中指定false将返回用大括号括起来的 UID(“Windows 样式”)。指定true或 nothing 将返回不带大括号的 UID。

兼容性

支持 4.2 以上版本的 PHP。它与操作系统无关,将根据操作系统、PHP 版本和可用的 PHP 库/函数(包括在 PHP Windows 中未加载 dotnet 库时调用回退选项)选择“最佳”方法。

编码

function GUIDv4 ($trim = true)
{
    $lbrace = chr(123);    // "{"
    $rbrace = chr(125);    // "}"

    // Windows
    if (function_exists('com_create_guid') === true) 
    {   // extension=php_com_dotnet.dll 
        if ($trim === true)
        {           
            return trim(com_create_guid(), '{}');
        }
        else
        {
            return com_create_guid();
        }
    }


    // OSX/Linux and Windows with OpenSSL but without com classes loaded (extension=php_com_dotnet.dll loaded in php.ini)
    if (function_exists('openssl_random_pseudo_bytes') === true) 
    {

        $data = openssl_random_pseudo_bytes(16);
        $data[6] = chr(ord($data[6]) & 0x0f | 0x40);    // set version to 0100
        $data[8] = chr(ord($data[8]) & 0x3f | 0x80);    // set bits 6-7 to 10
        if ($trim === true)
        {                   
            return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
        }
        else
        {
            return $lbrace.vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)).$rbrace;
        }
    }

    // Fallback (PHP 4.2+)      
    mt_srand((double)microtime() * 10000);
    $charid = strtolower(md5(uniqid(rand(), true)));
    $hyphen = chr(45);                  // "-"
    $guidv4 = substr($charid,  0,  8).$hyphen.
              substr($charid,  8,  4).$hyphen.
              substr($charid, 12,  4).$hyphen.
              substr($charid, 16,  4).$hyphen.
              substr($charid, 20, 12);

    if ($trim === true)
    {                   
        return $guidv4;
    }
    else
    {
        return $lbrace.$guidv4.$rbrace;
    }       
}

用法

$newGUID = GUIDv4([false]);  // false for braces, true or nothing for no braces

更多信息

http://php.net/manual/en/function.com-create-guid.php

http://php.net/manual/en/com.installation.php

http://guid.us/GUID/PHP

于 2016-12-06T11:19:18.700 回答