tags:

views:

257

answers:

2

I'm going to be uploading images to a system and need them to be referenced by a non-sequential unique ID. I've read a little about GUIDs, and I'm wondering what the best approach to making one in PHP is. Should I md5() the current timestamp and salt it, or will PHP's uniqueid (http://www.php.net/manual/en/function.uniqid.php) function be sufficient enough?

Thanks!

+3  A: 

Since uniquid uses the current time in microseconds to generate the guid, there is virtually no chance you'll ever run into the same one twice.

So if you're just using it to make unique filenames, uniqid() will be sufficient. If you want to prevent users from guessing the guid, you might as well make it harder and md5 it as well.

BraedenP
I like the idea of md5'ing it :)
Arms
But MD5 doesn’t preserve uniqueness.
Gumbo
If you md5 a key that's already unique, it'll remain unique.
BraedenP
@BraedenP: No, MD5 is a surjective function as it maps an infinite set of input values to an finite set of output values.
Gumbo
+2  A: 

GUID is Microsoft's version of UUID. PHP's uniqid is version 4 of UUID. Definitely good enough.

ZZ Coder