The method of generating standard uuid (guid) in php is explained in detail

  • 2021-12-05 05:55:20
  • OfStack

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

Typically, the platform will provide API that generates UUID. UUID is calculated according to the standard developed by the Open Software Foundation (OSF), using Ethernet card address, nanosecond time, chip ID code and many possible numbers.

A combination of the following parts: Current date and time (the first part of UUID is related to time. If you generate an UUID and then generate an UUID a few seconds later, the first part is different and 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, but no network card is obtained in other ways), and the only defect of UUID is that the generated result string will be relatively long.

The most commonly used standard for UUID is Microsoft's GUID (Globals Unique Identifiers).

In ColdFusion, UUID can be easily generated using the CreateUUID () function in the format of xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxxxxxx (8-4-4-16), where each x is a 106-ary number in the range of 0-9 or a-f.

The standard UUID format is: xxxxxxxx-xxxx-xxxx-xxxxxx-xxxxxxxxxx (8-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();
?>

Related articles: