Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
Advertisement
  1. Code
  2. PHP

How to Use Base64 Encoding in PHP

by
Difficulty:BeginnerLength:ShortLanguages:

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 into 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, 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 successful decoding, 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. 

Advertisement
Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.