How to Use Base64 Encoding in PHP

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 dat…


This content originally appeared on Envato Tuts+ Tutorials and was authored by Sajal Soni

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.

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.

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.

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.

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.

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. 


This content originally appeared on Envato Tuts+ Tutorials and was authored by Sajal Soni


Print Share Comment Cite Upload Translate Updates
APA

Sajal Soni | Sciencx (2021-04-19T14:14:10+00:00) How to Use Base64 Encoding in PHP. Retrieved from https://www.scien.cx/2021/04/19/how-to-use-base64-encoding-in-php/

MLA
" » How to Use Base64 Encoding in PHP." Sajal Soni | Sciencx - Monday April 19, 2021, https://www.scien.cx/2021/04/19/how-to-use-base64-encoding-in-php/
HARVARD
Sajal Soni | Sciencx Monday April 19, 2021 » How to Use Base64 Encoding in PHP., viewed ,<https://www.scien.cx/2021/04/19/how-to-use-base64-encoding-in-php/>
VANCOUVER
Sajal Soni | Sciencx - » How to Use Base64 Encoding in PHP. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/19/how-to-use-base64-encoding-in-php/
CHICAGO
" » How to Use Base64 Encoding in PHP." Sajal Soni | Sciencx - Accessed . https://www.scien.cx/2021/04/19/how-to-use-base64-encoding-in-php/
IEEE
" » How to Use Base64 Encoding in PHP." Sajal Soni | Sciencx [Online]. Available: https://www.scien.cx/2021/04/19/how-to-use-base64-encoding-in-php/. [Accessed: ]
rf:citation
» How to Use Base64 Encoding in PHP | Sajal Soni | Sciencx | https://www.scien.cx/2021/04/19/how-to-use-base64-encoding-in-php/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.