如何生成与数据库记录匹配的唯一 URL 变量?

发布于 2024-07-25 09:57:18 字数 281 浏览 1 评论 0原文

我希望能够生成这样的 URL 变量:

http://example.com/195yq
http://example.com/195yp
http://example.com/195yg
http://example.com/195yf

这些变量将与 MySQL 记录集匹配,以便我可以将其取出。 在创建该记录集时,我希望为其创建此密钥。

我怎样才能做到这一点? 做这个的最好方式是什么? 有没有我可以利用的现有课程?

谢谢大家

I wish to be able to generate URL variables like this:

http://example.com/195yq
http://example.com/195yp
http://example.com/195yg
http://example.com/195yf

The variables will match to a MySQL record set so that I can pull it out. At the time of creation of that record set, I wish to create this key for it.

How can I do this? What is the best way to do this? Are there any existing classes that I can make use of?

Thanks all

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

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

发布评论

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

评论(4

秋日私语 2024-08-01 09:57:18

基本上,您需要某种哈希函数。

这可以是 SHA-1 函数、MD5 函数(如 altCogito),或使用记录中的其他数据并对其进行编码。

你认为你会有多少记录? 选择解决方案时要小心,因为哈希函数必须足够大才能很好地覆盖您,但太大会创建比您需要的更大的数据库。 我使用了 SHA-1 并根据需要截断为 64 或 32 位。

另外,看看休息。 考虑一下 URL 可能不必那么神秘......

Basically, you need a hash function of some sort.

This can be SHA-1 function, an MD5 function (as altCogito), or using other data you have in the record and encoding it.

How many records do you think you will have? Be careful on selecting the solution as a hash function has to be big enough to cover you well, but too big and you create a larger database than you need. I've used SHA-1 and truncate to 64 or 32 bits, depending on need.

Also, look at REST. Consider that the URL may not have to be so mysterious...

只为守护你 2024-08-01 09:57:18

这可以通过几种方式非常简单地完成:

  • 使用哈希,例如 MD5。 (会很长)
  • Base-64 编码特定的 ID 或片段 。

如果你问是否有任何东西已经对 MySQL 记录做到了这一点,我不确定你会发现一些设计方面的东西,数据在概念上确实与你的 URL 相距甚远 网站,甚至在 Grails 这样的框架中也是如此。 大多数甚至不会尝试将前端和数据访问功能封装成一个整体。

This can be done pretty straightforward in a couple of ways:

  • Use a hash, such as MD5. (would be long)
  • Base-64 encode a particular ID or piece of data

If you're asking whether or not there is anything that does this already with MySQL records, I'm not sure that you'll find something as design-wise, data is really, really conceptually far away from the URLs in your website, even in frameworks like Grails. Most don't even attempt to wrap up front-end and data-access functionality into a single piece.

坏尐絯℡ 2024-08-01 09:57:18

您将在什么情况下使用它? 为什么不直接传递 post 变量呢? 它更加安全和整洁。 您仍然需要完成 url 中的数字,例如 id=195yq,并且可以通过配置 php.ini 文件来隐藏它们。

我希望这对您有所帮助。

请记住这一点。 当您在地址栏中传递变量时,其他人很容易更改变量并访问您可能不希望他们访问的信息。

In what context will you be using this? Why not just pass post variables? It is far more secure and neat. You will still accomplish the number in the url such as id=195yq, and there is a way to hide them by configuring your php.ini file.

I hope this can be of help to you.

Please keep this in mind. When you pass variables in the address bar it is easy for someone to change the variable, and access information you may not want them to access.

踏月而来 2024-08-01 09:57:18

在您提供的示例中,您似乎正在对数字主键进行 base-64 编码。 这就是我会这样做的方式,并且过去也这样做过,但我不确定这是否比以明文方式传递 ID 更好,因为 Base-64 解码很简单。

In the examples you gave, it looks like you're base-64 encoding the numeric primary key. That's how I would do it and have done it in the past, but I'm not sure it's any better than passing the ID in the clear because base-64 decoding is trivial.

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