制作一个类似于 TinyURL.com 的短 URL

发布于 2024-08-10 19:15:10 字数 180 浏览 4 评论 0原文

我正在构建一个新的 Web 应用程序,它需要生成一个内部短 URL,以便用户将来轻松返回到具有很长 URL 的特定页面。我最初的想法是在数据库中存储一个数字,并将其以十六进制值输出,以使其比整数短。 TinyURL.com 似乎使用了十六进制(多个大小写字母与数字混合)以外的其他内容。有没有一种简单的方法可以生成类似于 TinyURL 的东西?

I'm building a new web app that has a requirement to generate an internal short URL to be used in the future for users to easily get back to a specific page which has a very long URL. My initial thoughts are to store a number in a database and output it in a HEXADECIMAL value to keep it shorter than an integer. TinyURL.com seems to use something other than HEXADECIMAL (multiple case letters mixed with numbers). Is there an easy way to generate something similar what TinyURL does?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

瞄了个咪的 2024-08-17 19:15:10

请查看有关该主题的良好解释:Random TinyURL Browser (已更新)

重要部分:

正如我们所确定的,TinyURL 有 62,193,780 个可能的值。 TinyURL 由 Base 36 哈希生成(36 表示字符 az 和 0-9 的数量,可以构建 TinyURL 的可能值数组),由 MySQL 自动递增,初始值计数为零。

顺便说一句,另一个类似的问题,通过数学观点:创建你自己的 Tinyurl 样式 uid。这里是一些 .NET 源代码:Base 36 type for .NET (C#)

Please, check out this good explanation on subject: Random TinyURL Browser (Updated) .

Important part:

As we have established, there are 62,193,780 possible values for TinyURL's. TinyURL's are generated by a Base 36 hash (36 indicating the number of characters a-z and 0-9, the array of possible values out of which a TinyURL can be constructed), autoincremented by MySQL with an initial value count of zero.

BTW, another SO similar question, through a mathematical view : Creating your own Tinyurl style uid. And here some .NET source code: Base 36 type for .NET (C#)

青瓷清茶倾城歌 2024-08-17 19:15:10

它们使用 Base 36 编码,您可以使用 Base 64 使您的应用程序更加健壮。

这是我在 Python 中尝试的方法(我确实看到了您的语言标签,请原谅我):

#!/usr/bin/python

from base64 import b64encode
from hashlib import sha1

for i in range(5):
    salted_int = "<salt>%s</salt>" % i
    print b64encode(sha1(salted_int).hexdigest())[:6]

输出:

NTUwMz
ZTVmZD
OGEzNm
Njc2MT
YzVkNj

因此您可以自动递增一个整数并提供给它像这样的某种函数,最终很有可能得到一组随机的字符串。另请参阅我对此问题的回答。某些 base64 实现有可能发出斜杠 / 或加号 +,因此您应该在实现中留意这些,因为它们在网址。

哈希非常灵活,可以防止用户猜测下一个 URL(如果这对您很重要)。

They use base 36 encoding, and you can make your app more robust by using base 64.

Here's what I'd try in Python (I do see your language tags, forgive me):

#!/usr/bin/python

from base64 import b64encode
from hashlib import sha1

for i in range(5):
    salted_int = "<salt>%s</salt>" % i
    print b64encode(sha1(salted_int).hexdigest())[:6]

Outputs:

NTUwMz
ZTVmZD
OGEzNm
Njc2MT
YzVkNj

So you can autoincrement an integer and feed it to some kind of function like this, and end up with a good chance of a random group of strings. See also my answer to this question. Some base64 implementations have the potential to emit a slash / or a plus sign +, and therefore you should keep an eye out for these in your implementation as they're dangerous in URLs.

Hashes are really flexible and prevent your users from guessing the next URL (if this is important to you).

叹梦 2024-08-17 19:15:10

另一种 asp.net 开源供您研究:
迷你网址

Another asp.net open-source one for you to investigate:
mini url

难以启齿的温柔 2024-08-17 19:15:10

我最近在共享点的 codeplex 上看到类似的东西,他们似乎使用十六进制数字作为 url 缩短器。也许值得看看他们是如何做到这一点的 http://spurlshortener.codeplex.com/

I recently saw something like this on codeplex for sharepoint and they seemed to use hexadecimal numbers for the url shortener. It might be worth taking a look at how they do it here http://spurlshortener.codeplex.com/

梦初启 2024-08-17 19:15:10

我最初的想法是在数据库中存储一个数字,并将其以十六进制值输出,以使其短于整数

保留比整数短的值有什么意义?
所以你想要的网址如下:http://here.there/12D687而不是http://here.there/1234567

如果你问我哪一个对我来说更容易,我会告诉后一个。
但老实说,我看不出我的例子有什么意义,因为两者几乎相同。

是否有一种简单的方法可以生成类似于 TinyURL 的内容?

是的。 要求用户提供。
如果不可能,请使用纯整数 id。还有什么可以更容易...

My initial thoughts are to store a number in a database and output it in a HEXADECIMAL value to keep it shorter than an integer.

What is the point of keeping something shorter than an integer?
So you want to have URL like: http://here.there/12D687 instead of http://here.there/1234567?

If you'll ask me which one is easier for me I'll tell the latter one.
But honestly I do not see the point in my example as both are pretty much the same.

Is there an easy way to generate something similar what TinyURL does?

Yes. Ask the user to provide it.
If it is not possible just use the plain integer id. What can be easier...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文