views:

141

answers:

4

I need to compress a string so it is shorter for a GET method form. Is there any way to compress a string and it will be decrypted later? That way...

?error=LOTS OF STUFFLOTS OF STUFFLOTS OF STUFFLOTS OF STUFFLOTS OF STUFF

is shorter in some sort of key

?error=somekey

so I can get back the result later. Not using MySQL preferably.

Anyone know a good method for this?

Update: To clarify, I am using a GET because this is a cross site include and a POST will not be accepted into the variable scope of the HTTP included file.

+1  A: 

Easiest way to make your GET string shorter. Use POST.

(Update: Again, if you control how the form is sent, use POST. Use it. Don't use GET. To be clear, if you can use POST.)

But perhpas you need to pass this data as a regular old link. In that case I guess you could try php's compression functions. Some of the operate directly on strings.

For example, gzcompress() and gzuncompress() could be used to compress/uncompress a string. From the php manual:

<?php
  $compressed   = gzcompress('Compress me', 9);
  $uncompressed = gzuncompress($compressed);
  echo $uncompressed;
?>

Of course you'll have to run it through urlencode() and urldecode() - which since I'm sure the compression algorithms will output binary data, may not really save you anything.

Or it may not work at all. Would be interesting to try.

Update: Tested, it's crazy, but it did make your example string smaller.

Tim Lytle
OMG do you really believe compression could help here?
Col. Shrapnel
I'd never do it, but it's what he asked for - 'compress a string' 'decrypted' all in the question. I mean, it's an interesting theoretical concept.
Tim Lytle
I understand. Direct answer. No thinking involved.
Col. Shrapnel
@Col I wouldn't say there was no thinking to my answer, as you can see, my first line is (and always was) to use post not get.
Tim Lytle
@Col Now if thinking was involved in the asking. Not sure. I just took it at face value.
Tim Lytle
I appreciate the answer that actually in any way related to my original question. @Col this is not my first script... but its the first time I have ran into an issue like this. If $_POST worked I would be using it... but the HTTP include will not accept the variable scope from the page its being included in.
sct
@sct there is no thing called "HTTP include". go figure. You just include an URL which return plain HTML. Nothing else. I told you already to learn HTTP protocol. You can use POST as well, if you realize at last the fact, that there is no include but just HTTP requests.
Col. Shrapnel
I am referring to using the HTTP protocol when including in PHP which doesnt include the variable scope. If I can POST when including... how do I do that then? You are telling me that there is another way but I have no clue how to do that.
sct
+1  A: 

If you're using PHP, the easiest way to send an error message is with the $_SESSION. Simply say session_start(); at the top of the pages, and say $_SESSION['error'] = "TEXT";. Then isset($_SESSION['error']);.

Of course, you could always use $_POST.

Josh K
no, he can't use a session because of previous question
Col. Shrapnel
Obviously this sort of information should be included in this question.
Josh K
Correct point..
Col. Shrapnel
After checking the other question, I find the manner in which this is being coded to be **abysmal**. Due to this, that's why he can't use sessions.
Josh K
I changed to a GET because I could think of no easy way to do this besides a GET... but then I ran into the issue of... well these could get REALLY long and crappy. A way to encode/compress it would be awesome though if such a way existed. Thus why this question was made!
sct
You need to dump your current model and recode this.
Josh K
A: 

Not really 'on-the-fly', You might be able to Gzip and then base64 encode, (but base64 encoding increases the size, I just don't know how much)

But really, if you are exceeding the GET size, you should probably just switch to POST.

webdestroya
+1  A: 

I'd use POST instead... Or, come up with your own key mapping (error=1 would map to a long wordy error - like Col. Shrapnel's example).

You could also use a hash table. http://en.wikipedia.org/wiki/Hash_function

liquidleaf
wump
Key mapping would be stupid and considered code bloat unless the set of errors was extremely small (under 5).
Josh K
Not necessarily. Lots of ways to do it. You could have an infinite number of errors and still use keymapping. For example, have error # 203 read in an external file with the text of the error.
liquidleaf
If he can't find a way around long `$_GET` strings would he want / be able to setup file mapping to various static files? Not to mention increased I/O vs. using a session. I'm going to look into why he can't use a session.
Josh K