Real unique id
Juni 23rd, 2007 by Blu:RayNe
Every PHP programmer should know that.
I stumble upon too many lame md5-hashes based on md5(rand()) or even more ciritcal crc32(rand()). That could really lead to trouble especially when you’re using a lame unique id for critical applications like payments or somethimg. The chance that one id is used two times is not great, but if there is a chance for such an error, a programmer has to eleminte it!
And it’s so easy with PHP!
string uniqid ( [string $prefix [, bool $more_entropy]] )
Gets a prefixed unique identifier based on the current time in microseconds.
// no prefix
// works only in PHP 5 and later versions
$token = md5(uniqid());
// works only in PHP 5 and later versions
$token = md5(uniqid());
// better, difficult to guess
$better_token = md5(uniqid(rand(), true));
For more see the PHP Manual…
Filed under PHP having