views:

225

answers:

7

Hi Everyone,

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&param2=value2&param3=value3&param4=value4&param5=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.

+2  A: 

Add static values to the $_SESSION[].

soulmerge
A: 
  • 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.

bignose
+1  A: 

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.

Lucas
A: 

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&param2=sierra&param3=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.

brianpeiris
A: 

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&param2=2&param3=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.

Robin Day
A: 

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.

schooner
I think schooner's suggestion will work for me as I am going to send these links inside emails, so there's no way to keep them stored in session, etc.I will give it a try, thanks.
TamTam
A: 

I usually make something like this to reduce params:

Your link:

http://example.com/script.php?param1=value1&param2=value2&param3=value3&param4=value4&param5=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

Tom Schaefer