PHP 7.0.6 Released

com_create_guid

(PHP 5, PHP 7)

com_create_guidGenerate a globally unique identifier (GUID)

Description

string com_create_guid ( void )

Generates a Globally Unique Identifier (GUID).

A GUID is generated in the same way as DCE UUID's, except that the Microsoft convention is to enclose a GUID in curly braces.

Return Values

Returns the GUID as a string.

See Also

  • uuid_create() in the PECL uuid extension

User Contributed Notes

Alix Axel
5 years ago
The phunction PHP framework (http://sourceforge.net/projects/phunction/) uses the following function to generate valid version 4 UUIDs:

<?php

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));
}

?>

The output generated by the sprintf() and mt_rand() calls is identical to com_create_guid() results.
pavel.volyntsev(at)gmail
8 months ago
Use more cryptographically strong algorithm to generate pseudo-random bytes and format it as GUID v4 string

function guidv4()
{
    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); // set version to 0100
    $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
    return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
indrora
5 months ago
If you're going to generate random UUIDs, at least make them conform:

* The uppermost byte of the third stanza must be 4
* the uppermost byte of the fourth stanza may be any of (8 9 a b)

see also: The wikipedia page for UUIDs: https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_.28random.29
Dave Pearson (dave at pds-uk dot com)
17 days ago
Here's my final version of a GUIDv4 function (based on others work here) that should work on all platforms and gracefully fallback to less cryptographically secure version if others are not supported...

<?php
/**
* Returns a GUIDv4 string
*
* Uses the best cryptographically secure method
* for all supported pltforms with fallback to an older,
* less secure version.
*
* @param bool $trim
* @return string
*/
function GUIDv4 ($trim = true)
{
   
// Windows
   
if (function_exists('com_create_guid') === true) {
        if (
$trim === true)
            return
trim(com_create_guid(), '{}');
        else
            return
com_create_guid();
    }

   
// OSX/Linux
   
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
       
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
    }

   
// Fallback (PHP 4.2+)
   
mt_srand((double)microtime() * 10000);
   
$charid = strtolower(md5(uniqid(rand(), true)));
   
$hyphen = chr(45);                  // "-"
   
$lbrace = $trim ? "" : chr(123);    // "{"
   
$rbrace = $trim ? "" : chr(125);    // "}"
   
$guidv4 = $lbrace.
             
substr($charid08).$hyphen.
             
substr($charid84).$hyphen.
             
substr($charid, 124).$hyphen.
             
substr($charid, 164).$hyphen.
             
substr($charid, 20, 12).
             
$rbrace;
    return
$guidv4;
}
?>
mark at briley dot com
1 year ago
I made a mistake on my previous note.  I used rtrim instead of just trim to take off the curly brackets.  Here is my note again - or please change the RTRIM to just TRIM in my previous note.  Thanks!  :-)

Kristof_Polleunis at yahoo dot com has an excellent function for obtaining a GUID but I believe it can be improved by checking for whether or not to remove the curly braces so the programmer who uses it doesn't have to do this.  Here is the improved function:

<?php
#
#   Taken from the PHP documentation website.
#
#   Kristof_Polleunis at yahoo dot com
#
#   A guid function that works in all php versions:
#   MEM 3/30/2015 : Modified the function to allow someone
#       to specify whether or not they want the curly
#       braces on the GUID.
#
function guid( $opt = true ){       //  Set to true/false as your default way to do this.

   
if( function_exists('com_create_guid') ){
        if(
$opt ){ return com_create_guid(); }
            else { return
trim( 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 );    // "-"
           
$left_curly = $opt ? chr(123) : "";     //  "{"
           
$right_curly = $opt ? chr(125) : "";    //  "}"
           
$uuid = $left_curly
               
. substr( $charid, 0, 8 ) . $hyphen
               
. substr( $charid, 8, 4 ) . $hyphen
               
. substr( $charid, 12, 4 ) . $hyphen
               
. substr( $charid, 16, 4 ) . $hyphen
               
. substr( $charid, 20, 12 )
                .
$right_curly;
            return
$uuid;
            }
}

?>

As the comments say - set the incoming option ($opt) to either TRUE or FALSE depending upon whether you want the curly braces to always show up or not.  If you need to obtain a GUID in the other way - all you have to do is to send the opposite value to the function.
Kristof_Polleunis at yahoo dot com
11 years ago
A guid function that works in all php versions:

<?php
function guid(){
    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;
    }
}
echo
guid();
?>
To Top