如何在 PHP 中创建唯一键值?
如何在 PHP 中创建唯一的键值?
我需要长度为 20 位(不超过 33 位)的简单唯一密钥。
How do I create unique key value in PHP?
I need simple unique keys with a length of 20 digits (not more than 33).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您正在寻找加密密钥吗?
请勿在涉及安全的任何环境中使用
uniqid()
。在这种情况下使用它的可预测性太高了。PEAR::Crypt_RSA 包允许您在 PHP 中生成加密强密钥。一旦安装在本地 PEAR 安装中,您就可以使用它来生成密钥,如下所示:
UUID 也可能对您的目标有好处。下面是一个将在所有系统上生成符合标准的 UUID 的类:
不要在涉及安全的任何上下文中使用
uniqid()
。在这种情况下使用它太容易预测了。You are looking for an encryption key?
Do not use
uniqid()
in any context where security is involved. It is too predictable to be used in such context.The PEAR::Crypt_RSA package allows you to generate cryptographically strong keys in PHP. Once installed in your local PEAR install, you may use it to generate a key as follows:
UUID's may also be good for your goal. Here is a class which will generate standard-compliant UUIDs on all systems:
Do not use
uniqid()
in any context where security is involved. It is too predictable to be used in such context.尝试 uniqid() http://php.net/manual/en/function.uniqid。 php
如果您可以使用 pecl,您可以尝试此模块:http://pecl.php。 net/package/uuid
如果你在网络服务器中使用 Ubuntu,还有一些东西: http://code.google.com/p/php-uuid/
Try uniqid() http://php.net/manual/en/function.uniqid.php
If you can use pecl you can try this module: http://pecl.php.net/package/uuid
Theres also something if you use Ubuntu in your web server: http://code.google.com/p/php-uuid/
使用 uniqueid()
string uniqid ([ string $prefix = "" [, bool $more_entropy = false ]] )
虽然如果它是唯一的,我不知道为什么你有最小长度要求。
Use uniqueid()
string uniqid ([ string $prefix = "" [, bool $more_entropy = false ]] )
Although if it's unique, I don't know why you have a minimum length requirement.
代码
文档
mt_rand()
md5
uniqid
免责声明
理论上存在碰撞的可能性,但可能性不大。 uniqid 和 mt_rand() 应该使它对于可能的用途来说足够难以猜测。尽管这会产生 32 个字符长的字符串。
(根据建设性意见进行编辑)
Code
Doc
mt_rand()
md5
uniqid
Disclaimer
There is a theoretical chance of collision, but not likely. The combination of uniqid and mt_rand() should make it unguessable enough for what ever use there may be. Though this results in a 32 char long string.
(Edited after constructive comments)
这将为您创建一个至少 20 个字符长的唯一 ID
this will create a unique id for you which is at least 20 chars long