如何生成与数据库记录匹配的唯一URL变量? [英] How to generate unique URL variables that match to a db record?

查看:81
本文介绍了如何生成与数据库记录匹配的唯一URL变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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?

谢谢

推荐答案

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

Basically, you need a hash function of some sort.

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

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

您认为您会有几条记录?选择解决方案时要格外小心,因为散列函数必须足够大以覆盖您,但是太大了,并且您创建的数据库超出了您的需要.我已经使用SHA-1并根据需要将其截断为64或32位.

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.

另外,看看REST.考虑到URL可能不必太神秘了...

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

这篇关于如何生成与数据库记录匹配的唯一URL变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆