Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I create my GUID?

Tags:

php

guid

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!

like image 572
Steven Mercatante Avatar asked Sep 19 '09 20:09

Steven Mercatante


2 Answers

EDIT:

Yikes! I forgot about this ancient answer of mine. To clarify confusion created by my naivety (consistent with the comments made below): MD5 (like most useful hashes, by their nature) are not injective, so their output is not guaranteed to be unique for all inputs.

If hash collisions are an issue (in this case, they are), using this technique will require checking, after hashing, whether an identical key has already been generated.


Since uniqid 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.

like image 157
BraedenP Avatar answered Oct 06 '22 13:10

BraedenP


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

like image 30
ZZ Coder Avatar answered Oct 06 '22 14:10

ZZ Coder