我正在构建一个新的 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?
发布评论
评论(5)
请查看有关该主题的良好解释:Random TinyURL Browser (已更新)。
重要部分:
顺便说一句,另一个类似的问题,通过数学观点:创建你自己的 Tinyurl 样式 uid。这里是一些 .NET 源代码:Base 36 type for .NET (C#)
Please, check out this good explanation on subject: Random TinyURL Browser (Updated) .
Important part:
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#)
它们使用 Base 36 编码,您可以使用 Base 64 使您的应用程序更加健壮。
这是我在 Python 中尝试的方法(我确实看到了您的语言标签,请原谅我):
输出:
因此您可以自动递增一个整数并提供给它像这样的某种函数,最终很有可能得到一组随机的字符串。另请参阅我对此问题的回答。某些 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):
Outputs:
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).
另一种 asp.net 开源供您研究:
迷你网址
Another asp.net open-source one for you to investigate:
mini url
我最近在共享点的 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/
保留比整数短的值有什么意义?
所以你想要的网址如下:http://here.there/12D687而不是http://here.there/1234567?
如果你问我哪一个对我来说更容易,我会告诉后一个。
但老实说,我看不出我的例子有什么意义,因为两者几乎相同。
是的。 要求用户提供。
如果不可能,请使用纯整数 id。还有什么可以更容易...
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.
Yes. Ask the user to provide it.
If it is not possible just use the plain integer id. What can be easier...