1

I'm looking for a simple solution to reduce the length of my GET input code (on my website).

The forms are handled in JavaScript and, using a function, made simpler for URL sharing. For example: multiple form inputs, with different names, are converted into a simple settings=1,0,0,1 and placed in the URL as GET - this data is then exploded/imploded in PHP to use/re-use the input.

But, given the size of the form, it can still get pretty bloated. I'd like to compress that same data in the JavaScript and then decompress it in the PHP, allowing considerably shorter URLs for sharing.

Think TinyUrl. I did some googling, but solutions were more complex than I'd like - this isn't an attempt to hide data, but simply make it shorter.

Update: A better example of what I want is to to convert a long GET input like: settings=1,1,1,1,1,1,1,1,1,1,1,1,1 into settings=s8djh.

  • What is it that you're trying to accomplish? – jeroenvisser101 Feb 11 '13 at 13:45
  • 2
    So you don't want to encrypt anything, but rather compress it? – gronostaj Feb 11 '13 at 13:46
  • 1
    Seems like the OP just want to shorten the URL. – Rubens Mariuzzo Feb 11 '13 at 13:46
  • 1
    What's your definition of long url? Do you need to generate the url clientside, or is a roundtrip to the server ok? Depending on your requirements a possible solution might be to have a key-value store serverside (as suggested as a possible solution), another possible solution is to compress the url using javascript at the client (ex gzip or similar, but that depends on what's a long url if it will make url shorter or not.) Another solution is to remove redundant data – Onkelborg Feb 11 '13 at 14:26
  • note the changes in the url on this website: http://www.finalesfunkeln.com/s3/ (when using the tool). i'd like something like this.. maybe i should just read over that javascript :\ ..assuming it's being used to compress values. – user1512064 Feb 11 '13 at 14:31
  • ^^ the url shortening is literally for the GET input only, which is handled in the javascript. – user1512064 Feb 11 '13 at 14:32

3 Answers3

1

You could create your own "short URLs" like this: Create a unique id from the parameters (for example a md5 hash) and store the id-parameter pair in the data store.

You can use a database, key-value-store like BerkeleyDB or a noSQL db or flat text file that stores two values.

If the MD5 hash is still too long, you have two options: 1) Just use a counter that is counted up for each parameter string you store. Note however, that each of the parameter combinations can then be reached by simple URL manipulation. The other option would be to create a random n-character string (n at least >= 4), look if it exists in the database. If it does not exist, use the string as the id. If it does exist, try 2-5 times with nerw strings and if they all exist, bump up n by one. As time passes, you will fill up the 4-char namespace, then the 5-char namespace, etc. The 6-char namespace (using upper- and lowercase and digits) allows for 62^6 combinations - a lot to fill up!

See this introductory article for some example code. It uses MD5 hashes encoded in base62 for the hashes, making them less than the usual 32 hexadecimal chars long.

Edit: If you don't want to use a data store, maybe using an actual compression algorithm like LZW in JavaScript may shorten long rows of identical parameters.

Community
  • 1
  • 1
chiborg
  • 26,978
  • 14
  • 97
  • 115
0

You might be able to remove redundant data from the url using some kind of fixed "scheme":

settings=1,0,0,1 might become: v=a&s=9

where a is the version of your scheme (if you need to change it in the future, and keep old url:s valid) and s are the values. If 1,0,0,1 represents booleans (1/0, true/false) then they can be stored in a single bit each. Wasting one character per bit is bad.. Pack them all in one bit, and send the result in hex: 4 bits per character.

This, however, makes everything more complicated when you need to change anything, this, however, depends on your scenario. But it's possible, and it's a really effective way of shortening the length of your url:s.

Onkelborg
  • 3,927
  • 1
  • 19
  • 22
-3

Use:

http://phpjs.org/functions/base64_encode/

in JS and decode it in php through built in base64_decode

Mudaser Ali
  • 3,989
  • 3
  • 25
  • 27