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.
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