PHP

PHP Base64 Encode/Decode Example

Encoding in base64 simply involves converting binary data to ASCII text. Encoding in base64 is done so as to easily and effectively transfer binary data over a network. One of its use case is transferring data in emails.
 
 
 
 
 
 
 
 
 


 
In this example we are going to learn how to encode and decode data in base64 using PHP.
For this example we will use:

  • A computer with PHP>= 5.5 installed
  • notepad++

1. Getting Started

It is not safe to transfer raw binary data over a network simply because the data can get corrupted before it gets to the desired destination. Binary data can be corrupted because the underlying protocol might think that you’ve entered a special character combination ( the underlying protocol can miss interpret the contents in the data being transffered through it) or some other reasons.

To avoid the corruption of data and unnneccessary headaches that accompanies it, encoding is required. If data is encoded then definitly it has to be decoded, so the receiving end can correctly manipulate it.
Base64 is one way of encoding and decoding raw binary data into ASCII text. Though base64 tends to increase the total size of the data ( by 30 percent) it is still widely used.

1.1 base64 data

Below is an example a string encoded in base64.

index2.html

<!DOCTYPE html> 
<html lang=en>
	<head>
	<style>
	html, body{
	width:100%;
	height:100%;
	margin:0%;
	font-family:"helvetica","verdana","calibri", "san serif";
	overflow:hidden;
	padding:0%;
	border:0%;
	}
	 
	</style>
	 		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/>
	
	<title>base64 encode and decode </title>
	
	</head>
	<body>

UGFsYXBhIHdlYiBzZXJ2ZXIgaXMgYSBtb2JpbGUgc2VydmVyIGZvciBhbmRyb2lkIG9wZXJhdGluZyBzeXN0ZW0uIFRoZSBwYWNrYWdlIGluY2x1ZGVzIHRoZSBMaWdodHRwZCB3ZWIgc2VydmVyLCBQSFAsIE15U1FMLCBhbmQgTVNNVFAuIEl0IHR1cm5zIHlvdXIgZGV2aWNlcyBiYXNlZCBvbiBhbmRyb2lkIG9wZXJhdGluZyBzeXN0ZW0gaW50byBhIHdlYiBzZXJ2ZXIgYW5kIGRhdGFiYXNlIHNlcnZlciwgc3VpdGFibGUgZm9yIGJ1aWxkaW5nIGR5bmFtaWMgd2ViIHNpdGVzLiBQV1MgdmVyc2lvbiAyLjAuMCBvciBsYXRlciBpbmNsdWRlcyBzdXBwb3J0IGZvciBkZXZpY2VzIHRoYXQgdXNlIEFSTSAmIEludGVsICh4ODYpIHByb2Nlc3Nvci4KClBhbGFwYSB3ZWIgc2VydmVyIGlzIGEgZ3JlYXQgc2VydmVyIGZvciBhbmRyb2lkIG9wZXJhdGluZyBzeXN0ZW0gYW5kIGEgZ3JlYXQgdG9vbCBmb3IgcGVvcGxlIHdobyBqdXN0IHN0YXJ0ZWQgbGVhcm5pbmcgcHJvZ3JhbW1pbmcgd2l0aCBQSFAgKG9yIGV2ZW4gUEhQIGd1cnVzKS4KCkl0IGlzIHZlcnkgZWFzeSB0byB1c2UsIHdpdGggb25lIHRvdWNoIHlvdSBjYW4gZ2V0IHRoZSB3ZWIgc2VydmVyIHVwIGFuZCBydW5uaW5nLiBXaXRoIGFub3RoZXIgdG91Y2ggeW91IGNhbiBnZXQgdGhlIGRhdGFiYXNlIHNlcnZlciB3b3JraW5nLg==

	</body>
	</html>

See line 24 for data encoded in base64.

index.html

<!DOCTYPE html> 
<html lang=en>
	<head>
	<style>
	html, body{
	width:100%;
	height:100%;
	margin:0%;
	font-family:"helvetica","verdana","calibri", "san serif";
	overflow:hidden;
	padding:0%;
	border:0%;
	}
	 
	</style>
	 		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/>
	
	<title>base64 encode and decode </title>
	
	</head>
	<body>

Palapa web server is a mobile server for android operating system. The package includes the Lighttpd web server, PHP, MySQL, and MSMTP. It turns your devices based on android operating system into a web server and database server, suitable for building dynamic web sites. PWS version 2.0.0 or later includes support for devices that use ARM & Intel (x86) processor.

Palapa web server is a great server for android operating system and a great tool for people who just started learning programming with PHP (or even PHP gurus).

It is very easy to use, with one touch you can get the web server up and running. With another touch you can get the database server working.
	

	</body>
	</html>

In between the body tags is the string that was encoded to base64.

1.2 Encoding to base64

Encoding data to base64 in PHP is quite easy, all we need to do is call the method base64_encode ( string $data ) . $data is the string we want to encode to base64.

index.php

<!DOCTYPE html> 
<html lang=en>
	<head>
	<style>
	html, body{
	width:100%;
	height:100%;
	margin:0%;
	font-family:"helvetica","verdana","calibri", "san serif";
	padding:0%;
	border:0%;
	}
	 
	</style>
	 		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/>
	
	<title>FootBall Manager</title>
	
	</head>
	<body>

	<?php
$e="Palapa web server is a mobile server for android operating system. The package includes the Lighttpd web server, PHP, MySQL, and MSMTP. It turns your devices based on android operating system into a web server and database server, suitable for building dynamic web sites. PWS version 2.0.0 or later includes support for devices that use ARM & Intel (x86) processor.

Palapa web server is a great server for android operating system and a great tool for people who just started learning programming with PHP (or even PHP gurus).

It is very easy to use, with one touch you can get the web server up and running. With another touch you can get the database server working.";

echo base64_encode ($e);// encodes the string in variable  $e into base64 and print the returned value

?>

	</body>
	</html>

1.3 decoding base64

To decode data encoded with base64 we use the function base64_decode($str). $str represents the encoded data.

index2.php

<!DOCTYPE html> 
<html lang=en>
	<head>
	<style>
	html, body{
	width:100%;
	height:100%;
	margin:0%;
	font-family:"helvetica","verdana","calibri", "san serif";
	padding:0%;
	border:0%;
	}
	 
	</style>
	 		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/>
	
	<title>FootBall Manager</title>
	
	</head>
	<body>

	<?php
$e="UGFsYXBhIHdlYiBzZXJ2ZXIgaXMgYSBtb2JpbGUgc2VydmVyIGZvciBhbmRyb2lkIG9wZXJhdGluZyBzeXN0ZW0uIFRoZSBwYWNrYWdlIGluY2x1ZGVzIHRoZSBMaWdodHRwZCB3ZWIgc2VydmVyLCBQSFAsIE15U1FMLCBhbmQgTVNNVFAuIEl0IHR1cm5zIHlvdXIgZGV2aWNlcyBiYXNlZCBvbiBhbmRyb2lkIG9wZXJhdGluZyBzeXN0ZW0gaW50byBhIHdlYiBzZXJ2ZXIgYW5kIGRhdGFiYXNlIHNlcnZlciwgc3VpdGFibGUgZm9yIGJ1aWxkaW5nIGR5bmFtaWMgd2ViIHNpdGVzLiBQV1MgdmVyc2lvbiAyLjAuMCBvciBsYXRlciBpbmNsdWRlcyBzdXBwb3J0IGZvciBkZXZpY2VzIHRoYXQgdXNlIEFSTSAmIEludGVsICh4ODYpIHByb2Nlc3Nvci4KClBhbGFwYSB3ZWIgc2VydmVyIGlzIGEgZ3JlYXQgc2VydmVyIGZvciBhbmRyb2lkIG9wZXJhdGluZyBzeXN0ZW0gYW5kIGEgZ3JlYXQgdG9vbCBmb3IgcGVvcGxlIHdobyBqdXN0IHN0YXJ0ZWQgbGVhcm5pbmcgcHJvZ3JhbW1pbmcgd2l0aCBQSFAgKG9yIGV2ZW4gUEhQIGd1cnVzKS4KCkl0IGlzIHZlcnkgZWFzeSB0byB1c2UsIHdpdGggb25lIHRvdWNoIHlvdSBjYW4gZ2V0IHRoZSB3ZWIgc2VydmVyIHVwIGFuZCBydW5uaW5nLiBXaXRoIGFub3RoZXIgdG91Y2ggeW91IGNhbiBnZXQgdGhlIGRhdGFiYXNlIHNlcnZlciB3b3JraW5nLg";

echo base64_decode ($e);// decode data encoded with base64 and print out the string returned




?>

	</body>
	</html>



2. Summary

We learnt about encoding and decoding data in base64. We learnt why it is neccessary or even compulsory to do it. We also carried out some practical examples using PHP functions to encode and decode with base64.

3. Download the source code

Download the source code for this tutorial:

Download
You can download the full source code of this example here: base64encodedecodeexample

 

Olayemi Odunayo

I am a programmer and web developer, who has experience in developing websites and writing desktop and mobile applications. I have worked with both schematic and schemaless databases. I am also familiar with third party API and working with cloud servers. I do programming on the client side with Java, JavaScript, html, Ajax and CSS while I use PHP for server side programming.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button