简体   繁体   English

创建唯一的对话号码

[英]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: 我正在考虑分配发送消息的人的Userid + microtime()如下所示:

$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? 此代码是否始终是唯一的数字,或者也可能意外地为后续消息生成相同的数字?

您可以使用uniqid ,它用于生成唯一ID。

为什么不使用UniqId()函数?

I think php uniqid is sufficient 我认为php uniqid就足够了

From PHP DOC 来自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 如果设置为TRUE,则uniqid()将在返回值的末尾添加额外的熵(使用组合的线性同余生成器),这会增加结果唯一的可能性

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 然而,在md5的32位十六进制值(16 ^ 32)中大约有3.402823669209387e + 38个不同的值,你的几率非常小,所有相同的重复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM