I\'m working on a web service which requires a new GUID()
passed as a reference to a method within the service.
I am not familiar with C#
According to Is there any difference between a GUID and a UUID?
GUID is Microsoft's implementation of the UUID standard.
So, here's a link to libraries, that let's you create UUIDs of the following types:
https://github.com/search?p=1&q=uuid+php&ref=cmdform&type=Repositories
I don't know exactly, which one C# is using, but that's at least something you can use if you're writing some piece of software and want to have universal unique identifiers.
My perfered choice was https://github.com/fredriklindberg/class.uuid.php because it is just a simple PHP file and the most rated one (https://github.com/ramsey/uuid) had to much dependencies on other libraries, but his may change soon (see https://github.com/ramsey/uuid/issues/20).
But if you really need a GUID (according to the Microsoft standard), they have a different generation process than these 4122. Wikipedia claims that
GUIDs and RFC 4122 UUIDs should be identical when displayed textually
http://en.wikipedia.org/wiki/Globally_Unique_Identifier#Binary_encoding
In most cases, you should be fine by going for one of the PHP libs for UUIDs. I don't think you're meddling with Microsoft Component Object Model (COM), don't you?
If you just need a very unique ID:
$uid = dechex( microtime(true) * 1000 ) . bin2hex( random_bytes(8) );
If ID's are generated more than 1 millisecond apart, they are 100% unique.
If two ID's are generated at shorter intervals, this would generate ID's that are 99.999999999999999999% likely to be globally unique (collision in 1 of 10^18)
You can increase this number by adding more digits, but to generate 100% unique ID's you will need to use a global counter.
if you really do need RFC compliance, this will pass as a valid version 4 GUID:
$guid = vsprintf('%s%s-%s-4000-8%.3s-%s%s%s0',str_split(dechex( microtime(true) * 1000 ) . bin2hex( random_bytes(8) ),4));
This follows the intention, but not the letter of the RFC. Among other discrepancies it's a few random digits short. (Add more random digits if you need it) The upside is that this is fast, compared to 100% compliant code. You can test your GUID here
You can try the following:
function GUID()
{
if (function_exists('com_create_guid') === true)
{
return trim(com_create_guid(), '{}');
}
return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
}
Source - com_create_guid
<?php
function guid(){
if (function_exists('com_create_guid') === true)
return trim(com_create_guid(), '{}');
$data = openssl_random_pseudo_bytes(16);
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
?>
GUID Generator
As an alternative to the above options:
$guid = bin2hex(openssl_random_pseudo_bytes(16));
It gives a string like 412ab7489d8b332b17a2ae127058f4eb
For googlers such as my self, I found this snipet more accurate:
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;
}
}
source http://guid.us/GUID/PHP