How to generate standard UUID (GUID) in PHP

Time:2020-1-4

UUID refers to the number generated on one machine, which ensures that it is unique to all machines in the same space-time.

Usually, the platform provides the API for generating UUID. UUID is calculated according to the standard set by the Open Software Foundation (OSF), using Ethernet card address, nanosecond time, chip ID code and many possible numbers.

It is composed of the following parts: current date and time (the first part of UUID is related to time, if you generate a UUID after a few seconds, the first part is different, the rest are the same), clock sequence, globally unique IEEE machine identification number (if there is a network card, it is obtained from the network card, and no network card is obtained in other ways), unique lack of UUID The trap is that the resulting string will be longer.

The most common use of UUID is Microsoft’s guids (global unique identifiers).

In ColdFusion, you can use the createuuid() function to simply generate UUIDs. The format is: XXXX xxxx-xxxx-xxxx – XXXX XXXXXXXX x (8-4-4-16), where each x is a hexadecimal number in the range of 0-9 or a-f.

The standard UUID format is: XXXX xxxx-xxxx-xxxx-xxxxx-xxxxxxxx (8-4-4-4-12)


<?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();
?>

The above is the detailed explanation and integration of the method of generating standard UUID (GUID) in PHP introduced by Xiaobian to you, hoping to help you. If you have any questions, please leave a message to me, and Xiaobian will reply to you in time. Thank you very much for your support of the developepaer website!