Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a new GUID?

Tags:

php

guid

I'm working on a web service which requires a new GUID() passed as a reference to a method within the service.

I am not familiar with C# or the GUID() object, but require something similar for PHP (so create a new object which from my understanding returns an empty/blank GUID).

Any ideas?

like image 768
mauzilla Avatar asked Feb 10 '14 07:02

mauzilla


People also ask

How do I make a new GUID?

Open Visual Studio->Tools->Create GUID->Registry Format->New GUID. It will create a new GUID every time you click New GUID.

How does a GUID get generated?

A GUID (globally unique identifier) is a 128-bit text string that represents an identification (ID). Organizations generate GUIDs when a unique reference number is needed to identify information on a computer or network. A GUID can be used to ID hardware, software, accounts, documents and other items.

How do I get the new GUID in SQL?

-- If you want to generate a new Guid (uniqueidentifier) in SQL server the you can simply use the NEWID() function. -- This will return a new random uniqueidentifier e.g. You can directly use this with INSERT statement to insert new row in table.


2 Answers

You can try the following:

function GUID() {     if (function_exists('com_create_guid') === true)     {         return trim(com_create_guid(), '{}');     }      return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535)); } 

Source - com_create_guid

like image 66
Michel Ayres Avatar answered Sep 20 '22 15:09

Michel Ayres


As an alternative to the above options:

$guid = bin2hex(openssl_random_pseudo_bytes(16)); 

It gives a string like 412ab7489d8b332b17a2ae127058f4eb

like image 39
Alexey Avatar answered Sep 20 '22 15:09

Alexey