Quantcast
Channel: PHP function to generate v4 UUID - Stack Overflow
Browsing latest articles
Browse All 20 View Live

Answer by chamod rathnayake for PHP function to generate v4 UUID

The uuid_create function, which is available in PHP 7.2 and later, can be used. $uuid = uuid_create(); echo $uuid;

View Article



Answer by notebook for PHP function to generate v4 UUID

Cryptographically secure UUID v4 for PHP >= 7.<?phpfunction uuid4() { /* 32 random HEX + space for 4 hyphens */ $out = bin2hex(random_bytes(18)); $out[8] = "-"; $out[13] = "-"; $out[18] = "-";...

View Article

Answer by DaveInMaine for PHP function to generate v4 UUID

Just an idea, but what I ended up doing to get a V4 GUID was to use the database server. I am using SQL Server, and in the scenario where I needed the GUID, I was already running a query, so I just...

View Article

Answer by simpleton for PHP function to generate v4 UUID

This could be simpler?$uuid = bin2hex(openssl_random_pseudo_bytes(16));for($cnt = 8; $cnt <=23; $cnt+=5) $uuid = substr($uuid, 0, $cnt) . "-" . substr($uuid, $cnt);echo $uuid . "\n";

View Article

Answer by Serhii Polishchuk for PHP function to generate v4 UUID

Use Symfony Polyfill / UuidThen you can just generate uuid as native php function: $uuid = uuid_create(UUID_TYPE_RANDOM);More about it, read in official Symfony blop post -...

View Article


Answer by Cristián Carrasco for PHP function to generate v4 UUID

// php version >= 7$uuid = vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex(random_bytes(16)), 4));

View Article

Answer by Danny Beckett for PHP function to generate v4 UUID

A slight variation on Jack's answer to add support for PHP < 7:// Get an RFC-4122 compliant globaly unique identifierfunction get_guid() { $data = PHP_MAJOR_VERSION < 7 ?...

View Article

Answer by Baracus for PHP function to generate v4 UUID

I'm sure there's a more elegant way to do the conversion from binary to decimal for the 4xxx and yxxx portions. But if you want to use openssl_random_pseudo_bytes as your crytographically secure number...

View Article


Answer by indextwo for PHP function to generate v4 UUID

Having searched for the exact same thing and almost implementing a version of this myself, I thought it was worth mentioning that, if you're doing this within a WordPress framework, WP has its own...

View Article


Answer by bish for PHP function to generate v4 UUID

If you use CakePHP you can use their method CakeText::uuid(); from the CakeText class to generate a RFC4122 uuid.

View Article

Answer by Arie for PHP function to generate v4 UUID

In my search for a creating a v4 uuid, I came first to this page, then found this on http://php.net/manual/en/function.com-create-guid.phpfunction guidv4(){ if (function_exists('com_create_guid') ===...

View Article

Answer by Hoan Dang for PHP function to generate v4 UUID

How about using mysql to generate the uuid for you?$conn = new mysqli($servername, $username, $password, $dbname, $port);$query = 'SELECT UUID()';echo $conn->query($query)->fetch_row()[0];

View Article

Answer by Jules for PHP function to generate v4 UUID

Anyone using composer dependencies, you might want to consider this library: https://github.com/ramsey/uuidIt doesn't get any easier than this:Uuid::uuid4();

View Article


Answer by ThorSummoner for PHP function to generate v4 UUID

on unix systems, use the system kernel to generate a uuid for you.file_get_contents('/proc/sys/kernel/random/uuid')Credit Samveen on https://serverfault.com/a/529319/210994Note!: Using this method to...

View Article

Answer by Ja͢ck for PHP function to generate v4 UUID

FormattingTo construct a UUIDv4 you can generate 128 bits worth of random data, patch a few fields to make the data comply with the standard, and then format it as hexadecimal groups.According to RFC...

View Article


Answer by Victor Smirnov for PHP function to generate v4 UUID

My answer is based on comment uniqid user comment but it uses openssl_random_pseudo_bytes function to generate random string instead of reading from /dev/urandomfunction guid(){ $randomString =...

View Article

Answer by Michael for PHP function to generate v4 UUID

Inspired by broofa's answer here.preg_replace_callback('/[xy]/', function ($matches){ return dechex('x' == $matches[0] ? mt_rand(0, 15) : (mt_rand(0, 15) & 0x3 | 0x8));},...

View Article


Answer by amgine for PHP function to generate v4 UUID

From tom, on http://www.php.net/manual/en/function.uniqid.php$r = unpack('v*', fread(fopen('/dev/random', 'r'),16));$uuid = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', $r[1], $r[2], $r[3], $r[4]...

View Article

Answer by William for PHP function to generate v4 UUID

Taken from this comment on the PHP manual, you could use this: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,...

View Article

PHP function to generate v4 UUID

So I've been doing some digging around and I've been trying to piece together a function that generates a valid v4 UUID in PHP. This is the closest I've been able to come. My knowledge in hex, decimal,...

View Article
Browsing latest articles
Browse All 20 View Live




Latest Images