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
.
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.
Use:
http://phpjs.org/functions/base64_encode/
in JS and decode it in php through built in base64_decode
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.