1. 程式人生 > >PHP: Fatal error:Call to undefined function com_create_guid()

PHP: Fatal error:Call to undefined function com_create_guid()

本地Windows環境執行正常,放在Linux伺服器上出現致命錯誤:

Fatal error:Call to undefined function com_create_guid()

原因是伺服器PHP版本較高(5.4),沒有內建支援com_create_guid()函式,

參見PHP官方網站安裝說明( 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"

所以要麼在php.ini中新增com擴充套件,要麼修改下獲取GUID函式的實現以相容各個PHP版本,如下所示:
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;
    }
}

參考連結:

by iefreer