PHP-uniqid(“")是生成唯一且顺序的密钥服务器端的良好实用解决方案吗? [英] PHP - Is uniqid("") a good practical solution to generate a unique and sequential key server side?

查看:116
本文介绍了PHP-uniqid(“")是生成唯一且顺序的密钥服务器端的良好实用解决方案吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部

我想在注册过程中生成服务器端用户ID.此ID应该是顺序的,以允许群集索引.显然,它也必须是唯一的.

I want to generate server side a user id during registration. This id should be sequential to allow for clustered indexing. It also needs to be unique, obviously.

在何种程度上可以使用uniqid("")?我不是在构建Google,我认为2个用户在同一微秒内进行注册的风险几乎为零-但是我没有任何实践经验可作为基础. SQL将拒绝重复的ID,并且我的php代码中会有一个循环,如果发生冲突/数据输入失败,该循环将继续发送新的注册条目,但这只是一个好方法,如果冲突很少见.

To what extent can I use uniqid("") for this? I am not building Google, and I assume that the risk of 2 users registering at the same microsecond is close to nill - but I have no practical experience on which to base myself. SQL will reject duplicate id's, and I can have a loop in my php code that will keep on sending new registration entries in case of collisions / data entry failures, but that's only a good approach if collisions are rare.

或者,我可以在uniqid("",TRUE)后面添加一个随机后记,但如果2个用户在同一微秒内注册,则密钥将不再是连续的.

Alternatively, I could add a random postcript with uniqid("",TRUE), but then if 2 users register during the same microsecond, the keys can not be sequential anymore.

解决我的难题的最佳实用方法是什么?我在想这个吗?

What is the best, practical approach to my conundrum? Am I overthinking this?

谢谢

JDelage

推荐答案

uniqid仅仅是

uniqid is nothing more than an interface to microtime (which is why it generates sequential IDs), and as such, it could be predictable and could create a duplicate.

此ID应该是顺序的,以允许群集索引.显然,它也必须是唯一的. [...]我是否在想这个?

This id should be sequential to allow for clustered indexing. It also needs to be unique, obviously. [...] Am I overthinking this?

大多数数据库,包括MySQL,都包含事务安全的序列生成器. MySQL的实现AUTO_INCREMENT 很漂亮该死的原始,但也有效. auto-inc主键可确保唯一性,更重要的是,不奇怪.

Most databases, including MySQL, include transaction-safe sequence generators. MySQL's implementation, AUTO_INCREMENT, is pretty darn primitive, but also effective. An auto-inc primary key would ensure uniqueness and is, more importantly, not weird.

也就是说,仅确保表中的id列是主键就可以完全防止重复的ID.

That said, just ensuring that the id column in the table is a primary key is complete defense against duplicate IDs.

这篇关于PHP-uniqid(“")是生成唯一且顺序的密钥服务器端的良好实用解决方案吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆