com_create_guid()函数在服务器端出错,但在使用php的本地环境中工作正常
正在php文件中生成guid。我正在使用com_create_guid()函数在服务器端出错,但在使用php的本地环境中工作正常,php,Php,正在php文件中生成guid。我正在使用com\u create\u guid()。它在本地主机上运行良好,但我在远程服务器上显示了以下错误 致命错误:在第6行调用未定义的函数com_create_guid() 我的代码是(仅guid部分) 任何想法您都可以手动创建GUI: function getGUID(){ if (function_exists('com_create_guid')){ return com_create_guid(); } els
com\u create\u guid()
。它在本地主机上运行良好,但我在远程服务器上显示了以下错误
致命错误:在第6行调用未定义的函数com_create_guid()
我的代码是(仅guid部分)
任何想法您都可以手动创建GUI:
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;
如下:
您必须运行低于5的PHP版本,否则必须在LINUX机器上运行,因为COM是基于windows的扩展 尝试此脚本并确保
echo function_exists('com_create_guid')
? "Yes" + com_create_guid()
: "Nope !"
;
要扩展上述部分正确答案,请执行以下操作: 取自 从PHP5.4.5开始,COM和DOTNET不再内置在PHP核心中。您必须在PHP.ini中添加COM支持
extension=php\u com\u dotnet.dll可能的根本原因
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
未加载php_com_dotnet.dll(检查php.ini文件)的Windows系统以及非Windows系统将无法使用com_create_guid()
解决方案
我汇编并修改了以下代码,作为我自己的一些想法和更改(如始终使用大括号支持)以及来自多个来源的大量建议的总结,这些建议用于实现支持大括号和非大括号UID创建的跨平台和跨PHP版本函数。在函数调用中指定false将返回一个用大括号括起来的UID(“Windows样式”)。指定true或nothing将返回不带大括号的UID
兼容性
支持4.2以上版本的PHP。它与操作系统无关,将根据操作系统、PHP版本和可用的PHP库/函数选择“最佳”方法(包括在PHP窗口中未加载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
用法
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
更多信息
要么您的远程服务器没有运行PHP 5,要么它运行Linux,然后您就不能使用COM扩展。我用500万ID尝试了5次,但没有得到任何重复,所以我认为这对于商业用途来说也足够了。事实上,您可以改用它:sprintf(“%04X%04X-%04X-%04X-%04X-%04X-%04X-%04X-%04X-%04X-%04X%04X%,mtu-rand(065535)、mt_rand(065535)、mt_rand(065535)、mt_rand(1638420479)、mt_rand(3276849151)、mt_rand(065535)、mt_rand(065535)、mt_rand(065535));这与com_create_guid()中使用的方法相同;美丽只是beautiful@LawrenceCherone从PHP7.1开始,这就不重要了,因为
rand
现在使用与mt\u rand
相同的PRNG。应用extension=PHP\u com\u dotnet.dll
似乎只在Windows服务器上工作,而不是在发布的Linux(Ubuntu 14)上