如何缩短 URL 查询参数?
我的脚本正在生成一个非常长的 URL,如下所示,我想知道如何使用算法或技术来缩短它?
我想将其缩短为如下所示: http://example.com/script.php?p=430x2920
我怎样才能执行此操作而不缓存数据库的原始链接?
提前致谢。
My script is generating a very long URL just like the one below and I wonder how this can be shorten with an algorithm or technique?
This is the long URL:
http://example.com/script.php?param1=value1¶m2=value2¶m3=value3¶m4=value4¶m5=value5
I want to shorten it to something like this:
http://example.com/script.php?p=430x2920
How can I do this with out caching the original link the database?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您始终可以只使用带有分隔符的单个参数,然后将其拆分回代码中。
http://example.com/script.php?p=1x2x3x4x5
带有 x 或无论您想要什么,它都不属于作为分隔符的可能值的一部分。
You could always just use a single param with a delimiter and then split it back out in code.
http://example.com/script.php?p=1x2x3x4x5
with x or whatever you want that isn't part of the possible values as a delimeter.
将静态值添加到
$_SESSION[]
。Add static values to the
$_SESSION[]
.您可以使用帖子来表达您的价值观。 但如果你真的需要 url 中的所有信息,我认为你应该开始实现 url 重写.... 这里是重写的开始。
You could use post for your values. But if you really need all the info inside the url I think you should start implementing url rewriting.... here is a start to rewriting.
如果您不想使用数据库来存储缩短(散列)URL 的查找表,那么您必须设计某种函数来将缩短的 URL 转换为全长 URL。
换句话说,您的全长 URL 必须具有可以被压缩为较短 URL 的属性。
例如,我可以将以下 URL
http://example.com/script.php?param1=saffron¶m2=sierra¶m3=4
压缩到
http://example.com/ script.php?p=p1.sa_p2.si_p3:4
如果我知道 param1 和 param2 只接受某些关键字,而 param3 只接受数字。
If you don't want to use a database to store a lookup table of shortened (hashed) URLs then you'll have to devise some sort of function to transform a shortened URL to a full-length one.
In other words your full-length URL has to have properties such that it can be compressed into a shorter one.
For example I could compress the following URL
http://example.com/script.php?param1=saffron¶m2=sierra¶m3=4
into
http://example.com/script.php?p=p1.sa_p2.si_p3:4
If I knew that param1 and param2 only accept certain keywords and param3 only accepts numbers.
使用RESTful界面,而不是一堆查询参数。
存储将在服务器上的整个会话中持续存在的值,由会话 cookie 进行键控。
Use a RESTful interface, instead of a bunch of query parameters.
Store values that will persist across the session on the server, keyed by a session cookie.
计算出完整的可能值集,并提出一种双向算法来对它们进行编码/解码。
例如,如果您有 3 个参数,并且它们只是单位数字整数,那么您可以使用 ?123 并按每个字符拆分查询字符串来获取每个参数,而不是 ?param1=1¶m2=2¶m3=3 。
但这的可能性完全取决于您期望的值类型。
Work out the full set of possible values and come up with a two way algorithm to encode/decode them.
For example, if you have 3 parameters and they are only ever single digit integers then instead of ?param1=1¶m2=2¶m3=3 you can have ?123 and split the query string by each character to get each parameter.
How possible this is though is entirely dependent on what sort of values you are expecting.
我通常会做这样的事情来减少参数:
你的链接:
我的最爱:
或
http://example.com/script.php?params=值1|值2|值3|值4|值5
I usually make something like this to reduce params:
Your link:
My Favorites:
or
http://example.com/script.php?params=value1|value2|value3|value4|value5