Skip To Main Content | Home | Sitemap | Contact
ardor-protection

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: {B4925CB2-C8EA-4557-B110-E11947DF5FED}


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;





This Weeks Most Popular Pages Newest Pages
PrintBookmarkComment