Mat
Mat

Reputation: 6324

Create a Unique Conversation Number

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

Answers (3)

Baba
Baba

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

JvdBerg
JvdBerg

Reputation: 21856

Why not use the UniqId() function?

Upvotes: 1

xdazz
xdazz

Reputation: 160853

You could use uniqid, which is used to generate a unique ID.

Upvotes: 2

Related Questions