/** * SimpleQueryCoder.php3 * * Author: Martin Karas * Date: 3. 6. 2001 * * This should be used to encode & decode query string * sent by client through HTTP/GET method ie. easily * eavesdroppable text. To encode we first make BIT-XOR * on the input, then we encode the binary string using * standard base64 function (converts binary string into * plain text), which is then encoded through standard * urlencode function (substitutes URL-specials into %XX, * where XX is hex ASCII value of the special). * * To decode, simply use reverse order of these functions. * (Remember to use "decode" variants not "encode" !) * * All functions are bijective, so we will loose no * characters after decoding. **/ function SimpleQueryEncode($str) { return urlencode(base64_encode(~$str)); } function SimpleQueryDecode($str) { return ~base64_decode(urldecode($str)); } ?>
Example follows:
$s = "`1234567890-=\\~!@#$%^&*()_+|][';/.,}{\":?><".
"Toto je taky maly blby text na testovanie";
// pouzijeme HtmlSpecialChars(), aby sa nam to zobrazilo v browseri
// korektne, vplyv na kodovanie to nema
echo "Original:\t".HtmlSpecialChars($s)."\n".
"Encoded:\t".HtmlSpecialChars(SimpleQueryEncode($s))."\n".
"Decoded:\t".HtmlSpecialChars(SimpleQueryDecode(SimpleQueryEncode($s)))."\n";
?>