Thursday, January 22, 2015

Encode Dan Decode PHP mudah

I gives us our third function, gzdeflate() is used to “compress the given string using the DEFLATE data format.”
Again, not gonna veer off — let’s stay focused with a quick example.


Let’s say we want to “gzdeflate” the following string:
<?php $string = 'Encoding and Decoding Encrypted PHP Code'; ?>
We run this string through gzdeflate() and set it as a variable named $compressed:
<?php $compressed = gzdeflate($string); ?>
Echoing the $compressed variable to the browser, we get this bizarre-looking gibberish:


s�K�O��KWH�KQpI�r\���*JRS��SR

To “decode” this alien-speak, we inflate it with the converse function, gzinflate(), to restore the original string. Here is an example that returns the original string to a variable named$uncompressed:
$uncompressed = gzinflate(gzdeflate($string));

Echoing $uncompressed, we see the original string as expected:
Encoding and Decoding Encrypted PHP Code


AND
Copy/paste example:
<?php // gzinflate()/gzdeflate() example$string       = 'Encoding and Decoding Encrypted PHP Code';$compressed   = gzdeflate($string);$uncompressed = gzinflate($compressed);echo $compressed ."\n";echo $uncompressed;?>
Parameters
Also worth mentioning: whereas the first two functions — str_rot13() and base64_encode() — accept only one parameter (the input $string), the inflate/deflate functions accept twoparameters. Perhaps surprisingly, these second parameters are different between the two functions:
-gzdeflate() — level = the level of compression ( to 9)
-gzinflate() — length = the maximum length of data to decode
Returning to our example, let’s employ these second parameters to help visualize:


<?php // example using second parameters$string       = 'Encoding and Decoding Encrypted PHP Code';$compressed   = gzdeflate($string, 9); // compression level set to 9 = maximum$uncompressed = gzinflate($compressed, strlen($string)); // length set to same as $stringecho $compressed ."\n";echo $uncompressed;?>
And the result as displayed in a browser:
s�K�O��KWH�KQpI�r\���*JRS��SR

>Encoding and Decoding Encrypted PHP Code

No comments:

Post a Comment