PHP Classes

Better unique_id function

Recommend this page to a friend!

      Dreamhost API  >  All threads  >  Better unique_id function  >  (Un) Subscribe thread alerts  
Subject:Better unique_id function
Summary:Better unique_id function
Messages:1
Author:aitte
Date:2013-01-28 15:10:09
 

  1. Better unique_id function   Reply   Report abuse  
Picture of aitte aitte - 2013-01-28 15:10:09
Thanks for the nice wrapper library. It saved me some time.

Before discovering it, I had already had the time to begin writing my own, and wrote a function that generates proper UUIDs, which are much nicer than the uniqid() PHP output.

This function outputs what DreamHost really wants:

function generate_uuid()
{
// Calculate an MD5 of milliseconds & seconds since epoch, and a random number between 0 and getrandmax()
// Example: 0.84404300 1359380852 1828891132
$weak_uuid = md5( microtime( FALSE ) . rand() );
// Format like a UUID (turns 550e8400e29b41d4a716446655440000 into 550e8400-e29b-41d4-a716-446655440000)
$weak_uuid = substr( $weak_uuid, 0, 8 ) .
"-" . substr( $weak_uuid, 8, 4 ) .
"-" . substr( $weak_uuid, 12, 4 ) .
"-" . substr( $weak_uuid, 16, 4 ) .
"-" . substr( $weak_uuid, 20, 12 );
// Return the non-crypto-secure UUID (it's not random enough for serious security uses!)
return $weak_uuid;
}