UUID generated by php

Source: Internet
Author: User
UUID generated by php. UUID indicates that it is a universal Unique Identifier. it is a standard for Software construction and is also developed by the Open Software Foundation (OSF) organizations are part of the application in the Distributed Computing Environment (Distributed Computing Environment, DCE) field.

The purpose of UUID is to allow all elements in the distributed system to have unique identification information without specifying the identification information through the central control terminal. In this way, each user can create a UUID that does not conflict with others. In this case, duplicate names are not required during database creation. At present, the most widely used UUID is Microsoft's Globally Unique Identifiers (GUIDs, linux ext2/ext3 file system, LUKS encryption partition, GNOME, KDE, Mac OS X, and so on.

UUID refers to the number generated on a machine, which ensures that all machines in the same time and space are unique. Generally, the platform provides the generated API. According to the standards set by the Open Software Foundation (OSF), Ethernet card addresses, nanoseconds, chip ID codes, and many possible numbers are used.
UUID consists of the following parts:
(1) The current date and time. The first part of UUID is related to the time. if you generate a UUID after several seconds and then generate a UUID, the first part is different, the rest are the same.
(2) clock sequence.
(3) globally unique IEEE machine identification number. if there is a Nic, it is obtained from the nic mac address, and no Nic is obtained in other ways.
The unique defect of UUID is that the generated result string is long. The most common UUID standard is Microsoft's GUID (Globals Unique Identifiers ). In ColdFusion, you can use the CreateUUID () function to generate a UUID in the format of xxxxxxxx-xxxx-xxxxxxxxxxxxxxxx (8-4-4-16 ), each x is a hexadecimal number in the range of 0-9 or a-f.
The standard UUID format is: xxxxxxxx-xxxx-xxxxxxxxxx (8-4-4-4-12). you can download CreateGUID () UDF from cflib for conversion.

First, the PHP code is as follows: 

Function create_guid (){
$ MicroTime = microtime ();
List ($ a_dec, $ a_sec) = explode ("", $ microTime );
$ Dec_hex = dechex ($ a_dec * 1000000 );
$ Sec_hex = dechex ($ a_sec );
Ensure_length ($ dec_hex, 5 );
Ensure_length ($ sec_hex, 6 );
$ Guid = "";
$ Guid. = $ dec_hex;
$ Guid. = create_guid_section (3 );
$ Guid. = '-';
$ Guid. = create_guid_section (4 );
$ Guid. = '-';
$ Guid. = create_guid_section (4 );
$ Guid. = '-';
$ Guid. = create_guid_section (4 );
$ Guid. = '-';
$ Guid. = $ sec_hex;
$ Guid. = create_guid_section (6 );
Return $ guid;
}

Function ensure_length (& $ string, $ length ){
$ Strlen = strlen ($ string );
If ($ strlen <$ length)
{
$ String = str_pad ($ string, $ length, "0 ");
}
Else if ($ strlen> $ length)
{
$ String = substr ($ string, 0, $ length );
}
}

Function create_guid_section ($ characters ){
$ Return = "";
For ($ I = 0; $ I <$ characters; $ I ++)
{
$ Return. = dechex (mt_rand (0, 15 ));
}
Return $ return;
}

Echo create_guid ();

Second, the PHP code is as follows:

Function create_uuid ($ prefix = "") {// you can specify a prefix.
$ Str = md5 (uniqid (mt_rand (), true ));
$ Uuid = substr ($ str, 0, 8 ).'-';
$ Uuid. = substr ($ str, 8, 4 ).'-';
$ Uuid. = substr ($ str, 12, 4 ).'-';
$ Uuid. = substr ($ str, 16,4 ).'-';
$ Uuid. = substr ($ str, 20, 12 );
Return $ prefix. $ uuid;
}

Echo create_uuid ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.