com_create_guid() function got error on server side but works fine in local using php

前端 未结 4 1960
傲寒
傲寒 2020-12-10 11:00

working on generate a guid in a php file.i\'m using the com_create_guid(). It works fine on localhost but i shows the below error in remote server.

相关标签:
4条回答
  • 2020-12-10 11:06

    To extend the partially correct answers above:

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

    "From PHP 5.4.5, COM and DOTNET is no longer built into the php core. You have to add COM support in php.ini"

    extension=php_com_dotnet.dll

    0 讨论(0)
  • 2020-12-10 11:13

    You can create GUIDs manually:

    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;
        }
    }
    

    Usage:

    $GUID = getGUID();
    echo $GUID;
    

    Follow:

    http://guid.us/GUID/PHP

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

    0 讨论(0)
  • 2020-12-10 11:17

    Likely Root Cause

    Windows systems that do not have php_com_dotnet.dll loaded (check your php.ini file), as well as non Windows systems, will not be able to use com_create_guid().

    The Solution

    I assembled and modified the following code as the culmination some of my own ideas and changes (such as brace support throughout), and numerous suggestions from multiple sources for implementing a cross platform and cross PHP version function that supports braced and non-braced UID creation. Specifying false in the function call will return a UID wrapped in braces ("Windows style"). Specifying true or nothing will return a UID without braces.

    Compatibility

    PHP from version 4.2 upwards is supported. It is OS agnostic and will choose the "best" method based on OS, PHP version and what PHP libraries/functions are available (including calling a fallback option if the dotnet library isn't loaded in PHP Windows).

    The Code

    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;
        }       
    }
    

    Usage

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

    More Information

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

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

    http://guid.us/GUID/PHP

    0 讨论(0)
  • 2020-12-10 11:17

    You must be running PHP version less than 5 else you must be running on a LINUX box as COM is a windows based extension.

    Try this script and make sure.

    echo function_exists('com_create_guid')
        ? "Yes" +  com_create_guid()
        : "Nope !"
    ;
    
    0 讨论(0)
提交回复
热议问题