Skip To Main Content | Home | Sitemap | Contact
 

How To Generate A GUID Using PHP


PHP has the "com_create_guid" function. Since that is a COM function one can assume it leverages internal Window's functionality to obtain a GUID.  Obviously this is useful only on IIS. Refresh the page to obtain a new GUID.

What is a GUID? Excerpt from Wikipedia: "A globally unique identifier or GUID is a special type of identifier used in software applications to provide a reference number which is unique in any context (hence, "globally"), for example, in defining the internal reference for a type of access point in a software application, or for creating unique keys in a database. While each generated GUID is not guaranteed to be unique, the total number of unique keys (2128 or 3.4×1038) is so large that the probability of the same number being generated twice is extremely small."

GUIDs are useful in creating anti-CSRF tokens for web applications. Generate A GUID Using ASP.

This example is from the PHP.NET documentation as a user contribution from Kristof_Polleunis at yahoo dot com.  There is no disscussion or proof that it meets the basic requirements of being globally unique as defined in the definition.


GIUD: {03DB0892-164A-410D-9D7C-8415FE53144C}


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

Simple Use: $GUID = getGUID();
echo $GUID;



Latest Visitors: United States's flagUnited States's flagUnited States's flagUnited States's flagUnited States's flagUnited States's flagUnited States's flagUnited States's flagRussian Federation's flagChina's flagChina's flagUnited States's flagBrazil's flagIreland's flagIndia's flagUnited States's flagUnited Kingdom's flagGermany's flagUnited States's flagCanada's flagCanada's flagUnited States's flagBrazil's flagSweden's flagUnited States's flag


This Weeks Most Popular Pages Newest Pages
PrintBookmark
Comment