I'm looking to use a compression algorithm to store the target URL of a redirection script.
e.g., click.php?url=(compressed string)
Are there any compression functions that would give URL-friendly output? i.e. Output without any dangerous special characters; output that could be passed without fear of corruption when decompressing the $_GET value.
(and preferably functions that are available in PHP 4.3.1)
Only problem is that on the long URLs I tested, it seems that the base64encoded version of a compressed string is the same length as the base64encoded version of an uncompressed string; in other words, the base64encoding undoes the compression (in addition to increasing the string size by the standard 33%). And on shorter URLs, using base64 on a compressed string actually gives a longer string than using base64 on an uncompressed string. So it seems better to use base64_encode() without compression.
The compression functions I tried with base64_encode were gzcompress and gzdeflate; perhaps there are other functions better suited for this task.
why not just use urlencode() or rawurlencode()... that's what they're meant for.
I doubt you'll find any compression that can make a url take up less space, just because it is already so small, byte wise. Most compression algos are meant to make larger files smaller, but really don't make files that are already small much smaller (a little but not much). And a URL is pretty tiny already, so it is no surprise that gzipping a string might make it larger than the uncompressed counterpart.
If you're just after a shorter URL, you could store them in a database or text file and have the urls be like click.php?url=1 where 1 is the ID of the url.