In this quick article, we’ll discuss the basics of base64 encoding in the context of PHP. Basically, we’ll see how you could use PHP functions to transform data to the base64-encoded format.
What is Base64 Encoding?
Base64 is an encoding of binary data in ASCII. Each character in the string represents six bits of information. Effectively it represents the binary data in base 64 representation. Check out this primer on number systems and bases if you need a refresher.
The primary use of Base64 encoding is to encode binary data to ASCII text, when you want to transfer such data over protocols that are specifically designed to handle textual data. This makes sure that the data remains intact while it’s transported. Specifically, it’s Base64 is used for things like sending emails and uploading binary data in HTML forms.
Another common use of Base64 encoding is hashing. Of course, you’re not supposed to use Base64 encoding itself to generate hashes, since it can be easily decoded. Firstly, you generate a hash with the help of a hashing algorithm like SHA, and then you convert the resulting hash into the Base64-encoded format to display it. It’s really easy to compare two Base64-encoded checksums for integrity.
In the next section, we’ll discuss the built-in Base64 functions in PHP.
Base64 Functions in PHP
There are two main functions in PHP that deal with Base64 encoding. The base64_encode
function allows you to encode data in the MIME Base64 format. On the other hand, the base64_decode
function is used to decode the MIME Base64-encoded data.
Let’s go through each of these functions in detail.
base64_encode
Let’s go through the syntax of the base64_encode
function.
base64_encode ( string $string ) : string
The only argument you need to pass to the base64_encode
function is the source string which you want to encode to MIME Base64.
This returns a Base64-encoded string. It’s important to note that the Base64-encoded data takes around 33% more space in memory than the original data.
base64_decode
Let’s go through the syntax of the base64_decode
function.
base64_decode ( string $string , bool $strict = false ) : string|false
The first argument is the Base64-encoded data which you want to decode.
The second argument is optional, but if you pass TRUE
, it will perform strict checking. This means that if the Base64-encoded data contains characters from outside the Base64 alphabet, the decode function will return FALSE
. This is useful for checking the integrity of the Base64-encoded string. On the other hand, if you want to discard invalid characters silently, just pass FALSE
, which is the default value.
On a successful decode, the function returns the decoded string, otherwise it returns FALSE
. It’s important to note that the base64_decode
function may return binary data if the string in question was in the binary format before encoding.
In the next section, we’ll discuss how you can use built-in Base64 functions in PHP.
How to Encode and Decode Data With MIME Base64
In this section, we’ll go through a couple of examples to see Base64 functions in action.
Encode URL Parameters
More often than not, you’ll end up using the base64_encode
function to encode parameters that contain URLs.
Let’s quickly see how it works with the following example.
<?php $redirectUrl = 'https://www.example.com/some/other/url'; header('Location: https://www.example.com/redirect/' . base64_encode($redirectUrl)); //https://www.example.com/redirect/aHR0cDovL3d3dy5leGFtcGxlLmNvbS9zb21lL290aGVyL3VybA== ?>
As you can see, if we don’t use the base64_encode
function, the resulting URL would look like https://www.example.com/redirect/http://www.example.com/some/other/url
which is invalid.
Base64-Encoded Checksum
As we discussed earlier, the base64_encode
function comes in handy when converting binary data into an ASCII string.
Let’s have a look at the following example.
<?php $base64_encoded_hash = base64_encode(hash_hmac('sha1', 'Source string to be hashed.', 'YOUR_TOP_SECRET_KEY', true)); ?>
In the above example, the hash_hmac
function would return hashed binary data. So if you don't use the base64_encode
function, it would be difficult to display the resulting binary data.
Email Attachments
In PHP, when you send an email with file attachments, you can encode the file contents into the Base64-encoded format. In fact, if you’re sending binary attachments, then it’s essential that you encode the attachment data instead of sending it in the raw format.
Let’s have a look at the following example.
<?php $to = 'receiver@example.com'; $subject = 'Example of base64 encoded attachments'; $message = 'This email contains an image attachment.'; $boundary = md5(microtime()); $headers = "From: name <from@example.com>" . "\r\n"; $headers .= "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-Type: multipart/mixed; boundary=" . $boundary . "\r\n"; $headers .= "Multipart MIME example." . "\r\n"; $message.= "--" . $boundary. "\r\n"; $message.= "Content-Type: text/plain; charset=\"iso-8859-1\"" . "\r\n"; $message.= "Content-Transfer-Encoding: 8bit" . "\r\n"; $message.= $message . "\r\n"; // attach file $filename = 'nature.jpg'; $filecontents = file_get_contents($filename); $filecontents = chunk_split(base64_encode($filecontents)); $message.= "--" . $boundary. "\r\n"; $message.= "Content-Type: image/jpg; name=\"" . $filename . "\"" . "\r\n"; $message.= "Content-Transfer-Encoding: base64" . "\r\n"; $message.= "Content-Disposition: attachment" . "\r\n"; $message.= $filecontents . "\r\n"; $message.= "--" . $boundary. "--"; mail($mailto, $subject, $body, $headers); ?>
As you can see, we’ve used the base64_encode
function to encode binary image data before it’s sent as an attachment.
In this way, you can use Base64 functions in PHP for various purposes.
Conclusion
In this quick article, we discussed how you can use Base64 functions in your day-to-day PHP development.
No comments:
Post a Comment