Best way to generate a UUID in PHP without using external libraries

7.1k views Asked by At

I know there are tools like https://github.com/ramsey/uuid to generate UUID's but i dont use composer and would like to generate a UUID's in my login controller.

1

There are 1 answers

0
Niels Prins On BEST ANSWER

You can use this:

It uses random bytes and puts them in the right positions by using the vsprintf function.

Random bytes

$UUID = vsprintf( '%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex(random_bytes(16)), 4) );