How to create GUID in php

Rakesh Sharma      No Comments  

How to create GUID in php

How to create GUID in php

guid stands for globally unique identifier generally used to create random unique strings in php, create access token in php. It’s easy to generate or use guid in php.
In php we have built in function com_create_guid() to generate GUID. For use this we need COM support in php.ini. From PHP 5.4.5, COM and DOTNET is no longer built into the php core. You have to add COM support in php.ini.

uncomment this line from php.ini for this you have to remove semicolon(;) from beginning.
For get more information about COM follow manual.

If COM support is not enabled you will get fatal error on calling com_create_guid() :
Fatal error:Call to undefined function com_create_guid()

How to create guid in php using windows COM :-

$guid = com_create_guid();
echo $guid;

If you are not windows based platform and not have COM use below code for making GUID in PHP or remove Fatal error:Call to undefined function com_create_guid() manually by functions :-

function getGUID(){
    if (function_exists('com_create_guid')){
        return com_create_guid();
        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
            .chr(125);// "}"
        return $uuid;

and calling above function to print guid string:-

$GUID = getGUID();
echo $GUID;

For more information about GUID follow official GUID site Where you find more about this.
Mostly use of GUID for generating access token, generate unique id, generating unique string in php. Using this article how to create guid in php you can create a random string for any use to keep unique.