Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
The community reviewed whether to reopen this question 2 years ago and left it closed:
Opinion-based Update the question so it can be answered with facts and citations by editing this post.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(10)
这个想法是将唯一整数(例如当前时间)转换为其他数学基础。
PHP 中的一个非常简单的方法:
The idea is to convert a unique integer (such as current time) in an other mathematical base.
A very simple way in PHP :
一个小型 PHP 类,用于从一个或多个数字生成类似 YouTube 的哈希值。当您不想向用户公开数据库 ID 时,请使用 hashids。
来源:https://github.com/ivanakimov/hashids.php
A small PHP class to generate YouTube-like hashes from one or many numbers. Use hashids when you do not want to expose your database ids to the user.
Source: https://github.com/ivanakimov/hashids.php
这是一个每次随机生成唯一密钥的小函数。重复相同唯一 ID 的机会非常少。
Here is a small function that generates unique key randomly each time. It has very fewer chances to repeat same unique ID.
使用您喜欢的任何一个:-)
// 生成字母数字输出
// 只生成字母输出
Use whichever you like :-)
// Generates Alphanumeric Output
// Generates only alphabetic output
对我来说最好的算法是这样的: 使用 PHP/Python/Javascript/Java/SQL 创建类似 Youtube 的 ID
For me best algorithm is this: Create Youtube-Like IDs With PHP/Python/Javascript/Java/SQL
上面的所有方法都很好,但请确保您知道并且不要假设生成的字符串是唯一的。我过去所做的是创建一个递归函数,根据我的数据库检查字符串,如果它是唯一的,则返回该值,否则它会再次运行。不过,这几乎不会发生,具体取决于您的唯一字符串的长度。 知道它的独特性真是太好了。
all the methods above are good but make sure you know and not assume the generated string is unique. What I have done in the past is made a recursive function that checks the string against my database and if it is unique it returns the value, otherwise it just runs again. This will hardly happen though depending how long your unique string is. Its just good to know its unique.
我一直在寻找一种非常快速且简单的解决方案(这并不是为了以任何方式保证安全),它可以在重新生成时复制实际的哈希代码(与 PHP 的 microtime 唯一 id 不同)。这个就成功了。
http://php.net/manual/en/book.hash.php
所以:
产量:
等等
我不会假装知道如何使用所有不同的哈希算法,但对于非安全用途(如链接#anchors),这很方便。
I was looking for a very quick and simple solution (that was NOT intended to be secure in any way) that would replicate the actual hash code upon regeneration (unlike PHP's microtime unique id). This one did the trick.
http://php.net/manual/en/book.hash.php
So:
Yields:
etc
I won't pretend to know how all the different hash algorithms are used but for non-secure uses (like link #anchors) this is handy.
从此处的评论:
您可以使用此函数来生成字母数字,或者只是字母字符。
From comments on here:
You can mess around with this function to generate just alphanumeric, or just alphabetic characters.
对于大多数目的,
uniqid
就足够了,如果你需要的话为了确保绝对不发生冲突,需要采取更复杂的措施。For most purposes
uniqid
will suffice, if you need to make sure there's absolutely no clash, more convoluted measures are necessary.