Vu Quang Son's Blog

  • Home
  • Programming
    • Server
      • Centos
      • Ubuntu
    • PHP
      • WordPress
      • Laravel
    • GIT
    • SQL
  • Tools & Programs
    • Essential Programs
    • Handy Tools
      • Count character / letter online tool
      • Generate Random Number / Vietlott Ticket
      • SHA-512 Online Hash Generator
      • Image to base64 online converter
      • Bcrypt Hash Online Generator
      • Decrypt Bcrypt Hash Online
      • md5 Hash online generator
      • Online Version 4 UUID Generator
      • View Moon Phase Calendar Online
  • Funny
  • Tips & Tricks
  • Contact

PHP function to generate v4 UUID (Universally unique identifier)

Posted on August 23, 2018 by vuquangson2016 Posted in PHP .

Definition

A universally unique identifier (UUID) is a 128-bit number used to identify information in computer systems. The term globally unique identifier (GUID) is also used. When generated according to the standard methods, UUIDs are for practical purposes unique, without depending for their uniqueness on a central registration authority or coordination between the parties generating them, unlike most other numbering schemes. While the probability that a UUID will be duplicated is not zero, it is close enough to zero to be negligible.

Format

In its canonical textual representation, the sixteen octets of a UUID are represented as 32 hexadecimal (base 16) digits, displayed in five groups separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters (32 alphanumeric characters and four hyphens). For example:

123e4567-e89b-12d3-a456-426655440000
xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx

PHP Functions

The following function will generate v4 UUID

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 
 
function gen_uuid() {
    return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
        // 32 bits for "time_low"
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
 
        // 16 bits for "time_mid"
        mt_rand( 0, 0xffff ),
 
        // 16 bits for "time_hi_and_version",
        // four most significant bits holds version number 4
        mt_rand( 0, 0x0fff ) | 0x4000,
 
        // 16 bits, 8 bits for "clk_seq_hi_res",
        // 8 bits for "clk_seq_low",
        // two most significant bits holds zero and one for variant DCE1.1
        mt_rand( 0, 0x3fff ) | 0x8000,
 
        // 48 bits for "node"
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
    );
}
 
 

Test the functions

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
 
for ($i = 0; $i < 10; $i++) {
    echo gen_uuid() . "\n";
}
 
// OUTPUT
/*
4b0b138c-b0e8-49ef-a88d-0252555b50af
0fddfe3b-a2c3-45e0-8c13-c3ef9b345f20
d8567de3-eab1-49a3-8d26-b085a18ffc24
87dbb446-701a-4e5e-988d-e585ab04a5eb
5b10f3ac-b1bd-4042-abd8-b0208513d589
69680784-4e8f-47ea-984d-87eae9978215
39acac61-5f21-45ed-9dae-72b426c1c676
6ac52b79-37bd-44b6-b891-bccc523c7172
2323855e-2499-437d-8016-6eb07df28374
d84fee55-bcf1-4670-9924-d2342926be0e
*/
 
 

 

0 0
1 Comment
« PHP generate random unique string with custom length
Online Version 4 UUID Generator »

One Response

  1. furtdsolinopv says
    January 27, 2019 at 5:28 am

    Hiya, I’m really glad I have found this info. Nowadays bloggers publish just about gossips and net and this is actually frustrating. A good site with interesting content, that is what I need. Thanks for keeping this web-site, I’ll be visiting it. Do you do newsletters? Cant find it.

    Reply

Leave a comment

Leave a comment Cancel reply

Your email address will not be published. Required fields are marked *

Advertising

Recent Posts

  • View Moon Phase Calendar Online February 13, 2019
  • How to Disable Comments Notifications (Email) in WordPress January 28, 2019
  • Download Navicat Premium 12 full active January 23, 2019
  • How to create a Mircosoft Skype chat bot January 18, 2019
  • Validate unique field in laravel update request December 12, 2018

Recent Comments

  • furtdsolinopv on KISS, YAGNI & DRY Programming Principles
  • furtdso linopv on How to scale the enormously sized desktop icons in Ubuntu 18.04
  • furtdsolinopv on Conditional validation rules on laravel request
  • furtdso linopv on How to create a Mircosoft Skype chat bot
  • Arnon on License key PhpStorm 2018

Count per Day

  • 43478Total reads:
  • 33991Total visitors:
  • 43Reads today:
  • 38Visitors today:
  • 100Visitors per day:
  • 0Visitors online:

Recent Posts

  • View Moon Phase Calendar Online
  • How to Disable Comments Notifications (Email) in WordPress
  • Download Navicat Premium 12 full active
  • How to create a Mircosoft Skype chat bot
  • Validate unique field in laravel update request

Popular Posts

  • Activate Windows 10 Pro | KMSpico | Google Drive Link 141
  • License key PhpStorm 2018 133
  • Decrypt Bcrypt Hash Online 109
  • How to install Vietnamese input method on Ubuntu 18.04 LTS - ibus-unikey 55
  • How to show hidden files / folders on Ubuntu 18.04 LTS 22

Sitemap

  • Home
  • Programming
  • Tools & Programs
  • Funny
  • Tips & Tricks
  • Contact

Like Us on Facebook

Copyright © 2016 vuquangson.com

© Workable Solutions
  • Plugins Wordpress
  • Themes Wordpress
  • Documents Wordpress