问题
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.
回答1:
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.
回答2:
Add static values to the $_SESSION[]
.
回答3:
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.
回答4:
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.
回答5:
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.
回答6:
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.
回答7:
I usually make something like this to reduce params:
Your link:
http://example.com/script.php?param1=value1¶m2=value2¶m3=value3¶m4=value4¶m5=value5
My Favorites:
http://example.com/script.php?params=value1,value2,value3,value4,value5
or http://example.com/script.php?params=value1|value2|value3|value4|value5
来源:https://stackoverflow.com/questions/811403/how-can-i-shorten-the-url-query-paramters