将搜索参数保存和恢复为唯一 ID
菜鸟问题在这里。我正在彻底修改房地产网站中的一些“搜索”页面。我希望能够生成一个唯一的 ID (hash?) ,它本身包含搜索的所有参数,例如,将为用户提供一个 http://search.example.com/a95kl53df-02,加载此 URL 将重复完全相同的搜索。
有些搜索参数只是几个选项之一,有些是整数,还有关键字(我猜我只是将其附加在 ID 之后)。将这些数据填充到字符串中的一般方法是什么?我对 PHP/MySQL 相当熟悉,但我的实践经验几乎没有,所以我不知道“它是如何完成的”。
编辑:我不需要字符串是随机的,事实上,我需要过程是双向的。那么,也许哈希不是正确的术语。至于为什么 - 我这样做是为了简洁,因为当前 URL 至少包含 22 个 GET 参数。
我有一个坏习惯,就是总是过早地在互联网上提出问题,一旦我发布了帖子,重新考虑的想法就会突然出现在我的脑海中。我目前正在起草一个可能的解决方案。不过,我仍然愿意接受任何建议。
Noob question here. I'm overhauling some "Search" pages in a real estate website. I would like to be able to generate an unique ID (hash?) which contains in itself all the parameters of the search, e.g., the user would be given an URL in the form of http://search.example.com/a95kl53df-02, and loading this URL would repeat the exact same search.
Some of the search parameters are simply one of several options, some are integers, and there are also keywords (which I'll just append after the ID, I guess). What's the general approach to cramming this data into a string? I'm fairly comfortable with PHP/MySQL, but my practical experience is next to none, so I don't know "how it's done".
EDIT: I do not need the string to be random, and, indeed, I need the process to be two-way. Perhaps hash isn't the correct term, then. As for why - I'm doing this for the sake of brevity, since current URLs contain at least 22 GET parameters.
I have the nasty habit of always asking my questions on the Interwebs a bit too early, reconsiderations popping right into my head as soon as I have posted. I'm currently drafting a possible solution. I'm still open to any suggestions, though.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
哈希值不是唯一的
哈希值不是唯一的,您不能使用它。任何散列都可以由无限数量的给定字符串产生。
您不需要随机性,只需要一个唯一的令牌
您应该在数据库的帮助下生成一个唯一的令牌(甚至只是一个自动索引的 ID)。您可以创建一个 cronjob,在一段时间后删除旧的搜索。
该表至少包含唯一标记加上原始搜索字符串。
可能的实现
Hashes are not unique
A hash is NOT unique, you can't use it. Any hash can result from an infinite number of given strings.
You don't need randomness, just a unique token
You should just generate a unique token with the help of the database (even just an autoindexed id). You can create a cronjob that deletes old searches after a while.
That table would minimally contain the unique token plus the original search string.
Possible implementation
您可以在
$_SERVER['QUERY_STRING']
上使用类似mcrypt()
的方法,然后在传入加密 URL 时对其进行解密。但是,有各种各样的方法这里有问题,我建议不要这样做。根据您的编辑,您这样做是因为 URL 很复杂,我建议散列会使问题变得更糟。如果 URL 有错误,那么现在有多个地方可能出错。
只需创建一个随机密钥,然后在简单的平面文件数据库中查找即可。您可以检查该 URL 是否已在数据库中,如果存在则返回密钥。
该系统的另一个优点是,如果您的 URL 结构发生变化,那么您可以更改数据库中的所有 URL,并且用户的短 URL 仍然有效。
You could use something like
mcrypt()
on$_SERVER['QUERY_STRING']
, and then decrypt it if an encrypted URL is passed in. However, there are all sorts of problems here and I recommend not doing that.Based on your edit that you are doing this because of a complicated URL, I would suggest that hashing is going to make the problem worse. If you have an error with the URL, you now have multiple places it could be going wrong.
Just make a random key that you then lookup in a simple flat-file database. You could check whether the URL is already in the database and then return the key if it is.
Another advantage of this system is that if your URL structure changes, then you can change all the URLs in the database and the users' short URLs still work.
好吧,要
随机
(顺便说一下,你永远不可能),你可以散列让我们说微时间(这是随机的sh) ,因为 2 个用户同时搜索的可能性很低)以及一些盐,您可以使用的是查询 id:所以类似于:
更新
由于下面的评论,我提供了另一种解决方案(可能更
独特
):Well to be
random
(which by the way you never can be), you can hash let us say the microtime (which is random-sh, since there is a low possibility that 2 users will search at the same time) along with some salt, with what you can use is the query id:so something like:
UPDATE
Due to the comments below, I offer another solution (which can be more
unique
):