Reputation: 6324
I'm working on a private message system and I want to assign a unique conversation number so I can Identify if multiple users are taking part to the same conversation. I was thinking to assign the Userid of the person who send the message + microtime()
like this:
$conversation_number = md5($_POST['user_id']+microtime());
Would this code originate always a unique number or it may also possible that, accidentally, it generate the same number for the subsequent messages?
Upvotes: 1
Views: 96
Reputation: 95121
I think php uniqid
is sufficient
From PHP DOC
If set to TRUE, uniqid() will add additional entropy (using the combined linear congruential generator) at the end of the return value, which increases the likelihood that the result will be unique
try
uniqid('id', true)
However there are approximately 3.402823669209387e+38 different values in a 32 digit hex value (16^32) in md5
the your odds are phenomenally small that there will be a duplicate all the same
Upvotes: 1