Encode and Decode query string value in php

PHP Programming / URL Handling

May 22, 2017

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.

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:

<?php
$str = 'This is an encoded string';
echo base64_encode($str);
?>

Output:

VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==

base64_decode

base64_decode — Decodes data encoded with MIME base64

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:

<?php
$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
echo base64_decode($str);
?>

Output:

This is an encoded string

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts

Tags

Related Posts

Many new updates happened for Android developers lately after Google I/O. Initially there was no restriction on some features but now.
phpMyAdmin is a web-based database management tool that you can use to view and edit the MySQL databases on your EC2 instance.
Title attribute can be helpful to expand on the meaning of your navigation label and give your users more context You can provide to the link.