PHP 随机 URL 名称(短 URL)
使用 JSFiddle 等网站后,我注意到它们会自动生成一个由各种大小写字符组成的随机且唯一的 URL。
我们的预订页面可以从中受益。它是如何完成的?
After using sites like JSFiddle I noticed that they auto generate a random and unique URL made up of various upper and lower case characters.
We could benefit from this for our booking pages. How is it done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这不是随机的,而是基于数据库记录的 ID。
它是如何工作的:
基本上你有一个唯一的字符串,但它可以被解密以表示一个数字,你应该将其视为一个简短的加密/解密。
您有一个函数,它需要一个唯一的 ID,然后根据该 id 创建一个唯一的“短字符串”,然后您可以反转该过程以从该短字符串中获取唯一的 id。
这是我找到的一个网站的片段:
示例
这里有一个脚本链接:https://github.com/kvz/deprecated/blob/kvzlib/php/functions/alphaID.inc.php
This is not random, there based of the ID of your database record.
How it works:
basically you have a string that is unique but it can be decrypted to represent a number, you should look at it as a short encryption / decryption.
You have a function that would take an unique ID and then it creates a Unique 'short string' from that id, and then you can reverse the process to get the unique id from the short string.
Here's a snipped I have found of a website:
Example
there's a link to the script here: https://github.com/kvz/deprecated/blob/kvzlib/php/functions/alphaID.inc.php
如果您只想要指定的随机 ID,请使用 uniqid() 函数长度或使用长网址的 md5() 哈希(如果您需要一些可重复的东西。 (对于给定的输入,输出始终相同)。 使用 PHP 和 MySQL 创建短 URL 服务 很好地概括了如何将它们组合在一起。
Use the uniqid() function if you just want a random id of a specified length or use the md5() hash of your long url if you need something repeatable. (output always the same for a given input). Creating a short URL service using PHP and MySQL gives a good rundown of how to put it all together.
沿着这些思路:
请注意,这部分是伪代码。这将创建一个具有随机长度(介于 10 到 20 之间)的 URL,并使用与字母表中的字母相对应的随机 ASCII 代码填充每个字符。 ASCII 表中的大小写字母之间存在一些“垃圾”字符,因此这将重新滚动随机数,直到生成有效的 ASCII 数字。
这假设您将此 URL 存储在数据库中。为了确保该字符串是唯一的,您需要检查它是否存在于数据库中。如果没有,那么添加它就可以了。如果存在,则再次重复该过程,直到获得唯一的字符串。
Something along these lines:
Note this is partially psuedo code. This creates a URL with a random length (somewhere between 10 and 20), and populates each character with a random ASCII code that cooresponds to a letter in the alphabet. There are a few "junk" characters between the upper and lower case letters in the ASCII table, so this will reroll a random number until a valid ASCII number is generated.
This assumes you are storing this URL in a database. To ensure that this string is unique, you need to check to see if it exists yet in the database. If not, then add it and you're fine. If it exists, then redo the process again until you get a unique string.
这很简单。
您的
URL
类似于:www.example.com/in/here/RANDOMPART
您重写
www.example.com/in/here/*
> 到您选择的脚本。在此脚本中,您可以使用全局变量获取请求的URI
,并且可以将其拆分并使用最后一个/
之后的部分。这是您的RANDOMPART
。你需要根据你的数据库
或者其他什么来检查它。要创建这样的
URL
,您只需在www.example.com/in/here/
后面添加一些内容即可。您可以使用md5(uniqid(rand(), true))
生成一个漂亮且希望唯一的字符串。不要忘记将此字符串保存在数据库或其他任何位置。
我希望这有帮助。
It is very simple.
Your
URL
is something like:www.example.com/in/here/RANDOMPART
You rewrite
www.example.com/in/here/*
to a script of your choice. In this script you can get the requestedURI
with the globals and you can split it and use the part after the last/
. This is yourRANDOMPART
. yust check it against yourDatabase
or whatever.To create such an
URL
you simply have to add something behindwww.example.com/in/here/
. You could generate a nice hopefully unique string withmd5(uniqid(rand(), true))
.Do not forget to save this string in a
Database
or whatever.I hope that helps.
您可以简单地创建一个包含所有潜在字符的数组,然后随机挑选您想要的字符并将它们作为 URL“代码”返回?
You could simply create an array of all potential characters then randomly pick out however many you want and return them as the URL "code"?