php 客户端的唯一标识符
我有一个唯一的客户标识符,每次客户请求服务支持时都会生成该标识符。
一旦生成了id,该id就会被插入到数据库中
,我的代码:
function makeUnique() {
$start_time = uniqid(microtime(1));
$duration = sprintf('%0.24f', $start_time);
return date('dmYHis').$duration;
}
echo makeUnique();
此输出: 071120112032291320715949.928639888763427734375000
由于某种原因,我得到 071120112032291320715949 作为数字。我做错了什么?
I have a unique identifier for client i generate everytime a client request a service support.
once the id is generate, the id is inserted in the database
here my code:
function makeUnique() {
$start_time = uniqid(microtime(1));
$duration = sprintf('%0.24f', $start_time);
return date('dmYHis').$duration;
}
echo makeUnique();
this output: 071120112032291320715949.928639888763427734375000
for some reason i get 071120112032291320715949 as the number. what am i doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你需要删除“。” (点)在你的函数中。您可能正在使用 INT 来删除点后的数字。
return str_replace('.', '', date('dmYHis').$duration);
并确保该字段足够大 - 就像 varchar(50)
我的建议是简单地散列 客户端 ID
使用 md5
md5($clientid)
的并且您将 mysql 字段作为 char(32)
you need to remove the "." (dot) in your function. you are probably using an INT which remove numbers after the dot.
return str_replace('.', '', date('dmYHis').$duration);
and make sure the field is big enough - like varchar(50)
my recommendation will be to simply hash the client id using md5
md5($clientid)
and you have the mysql field as a char(32)
您已经在代码中找到了解决方案。 php 函数 uniqid() 提供了……嗯,一个唯一的 ID。如果您想确保它确实非常非常独特(尽管这不是必需的),只需将 time() 附加到末尾,如下所示:
这也会返回与数字混合的字母(更高的熵),如下响应:
You've already got your solution in your code. The php function uniqid() provides... well, a unique ID. If you want to ensure it's really really really really unique (though it's not necessary), just append time() to the end, like so:
This would also return letters mixed in with numbers (higher entropy), like this response: