There are many ways to encode and decode PHP code.From the perspective of site security, there are three PHP functions — str_rot13(), base64_encode(), and gzinflate — that are frequently used to encode and decode strings of PHP code.
Encode and decode with base64_encode() & base64_decode()
In this tutorial I will be explaining how to encode and decode query string value in PHP.
base64_encode
base64_encode — Encodes data with MIME base64.
1 |
string base64_encode ( string $data ) |
Encodes the given data with base64.
This encoding is designed to make binary data survive transport through transport layers that are not 8-bit clean, such as mail bodies.
Base64-encoded data takes about 33% more space than the original data.
The encoded data, as a string or FALSE on failure.
Examples:
1 2 3 4 |
<?php $str = 'This is an encoded string'; echo base64_encode($str); ?> |
Output:
1 |
VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw== |
base64_decode
base64_decode — Decodes data encoded with MIME base64
1 |
string base64_decode ( string $data [, bool $strict = false ] ) |
Decodes a base64 encoded data.
Parameters:
data
The encoded data.
strict
If the strict parameter is set to TRUE then the base64_decode() function will return FALSE if the input contains character from outside the base64 alphabet. Otherwise invalid characters will be silently discarded.
Returns the original data or FALSE on failure. The returned data may be binary.
Examples:
1 2 3 4 |
<?php $str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw=='; echo base64_decode($str); ?> |
Output:
1 |
This is an encoded string |