Generate a random GUID

Function generateGuid creates a random GUID (globally unique identifier).

MSDN defines GUID as "a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. Such an identifier has a very low probability of being duplicated."

GUID consists of alphanumeric characters only and is grouped in five groups separated by hyphens as seen in this example:
3F2504E0-4F89-11D3-9A0C-0305E82C3301

The source of the function is as follows:

  1. <?php
  2.  
  3. /**
  4.  * Generate Globally Unique Identifier (GUID)
  5.  * E.g. 2EF40F5A-ADE8-5AE3-2491-85CA5CBD6EA7
  6.  *
  7.  * @param boolean $include_braces Set to true if the final guid needs
  8.  *                                to be wrapped in curly braces
  9.  * @return string
  10.  */
  11. function generateGuid($include_braces = false) {
  12.     if (function_exists('com_create_guid')) {
  13.         if ($include_braces === true) {
  14.             return com_create_guid();
  15.         } else {
  16.             return substr(com_create_guid(), 1, 36);
  17.         }
  18.     } else {
  19.         mt_srand((double) microtime() * 10000);
  20.         $charid = strtoupper(md5(uniqid(rand(), true)));
  21.        
  22.         $guid = substr($charid,  0, 8) . '-' .
  23.                 substr($charid,  8, 4) . '-' .
  24.                 substr($charid, 12, 4) . '-' .
  25.                 substr($charid, 16, 4) . '-' .
  26.                 substr($charid, 20, 12);
  27.  
  28.         if ($include_braces) {
  29.             $guid = '{' . $guid . '}';
  30.         }
  31.    
  32.         return $guid;
  33.     }
  34. }

If you look at the source more closely you will see that this function generates a random MD5 hash and uses that as the source for the GUID. After it extracts the first 8 characters from the $charid, adds a dash, extracts 4 more and adds a dash (three times in a row) and then adds 12 final characters.

You can wrap the GUID in curly braces if you set $include_braces to true.

The only thing left to do is to return the value...

Comments
1
If com_create_guid exists the $include_braces param doesn't work (it actually gets 2 sets of braces) if it is true.

If com_create_guid is true and $include_braces is false, you should return str_replace( array("{", "}"), array('', ''), com_create_guid())

Still helped me out though - pretty easy to figure out what to do when you got too many brackets :)
Trent Tompkins, May 24th 2010, 4:39
2
You're right. When you mentioned it I realized that com_create_guid() adds braces automatically.

Snippet updated to reflect your suggestions :)
Andris, May 28th 2010, 9:28
3
nice script it working for me great script.
akhilesh, July 30th 2014, 11:45
4
well wisher saying this is very useful
well wisher, February 27th 2015, 12:56
5
This comment has been deleted
well wisher, February 27th 2015, 12:58
Name
Email (required)
will not be published
Website
Recaptcha
you will only be required to fill it in once in this session

You can use [code][/code] tags in your comments