X

Encode and Decode Query String value In PHP

Securing the URL is one of the biggest challenges in all-time because out of your territory there are many trespassers who will destroy your dream project with hacking, So In this post- Encode and Decode Query String value In PHP I will be going to explain how to secure URL query string with encoding and decoding the get data on any specific page.

when you write a piece of code in PHP and you want to pass that value in some other page what you will be going to use, maybe query string and if you are going query string, the URL will look

www.example.com/demo.php?id=2

with the help of this URL you will get the value of no. 2 id’s, suppose somebody change this ID then he will get another id’s data, means No data privacy is here. So PHP has two functions base64_encode() and base64_decode().

base64_encode() function encode the data and base64_decode() decode the encoded data.

base64_encode: This encoding is designed to make binary data survive transport through transport layers that are not 8-bit clean, such as mail bodies.

<?php
$str = 'TechJunkGigs';
echo base64_encode($str);
?>

result

VGVjaEp1bmtHaWdz

base64-decode: Decode the base64 data into humanly understandable language

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

result

TechJunkGigs

I’m going to write a small piece of code that take some data into variables after that the same data encoded and pass to another page and in another page, you with getting the decoded data.

In the above image if we don’t encode the data it will show users in URL bar

page1.php

In this page, I’m taking the name, email and phone number and store into variable after that I’m passing that value through a query string.

<?php
	$name = "TechJunkGigs";
	$email = "example@mail.com";
	$phone = "123456789";
	
	$enc_name = base64_encode($name);
	$enc_email = base64_encode($email);
	$enc_phone = base64_encode($phone);
?>
	<div style = "font-size:26px;color:red;">
		<p>Name  =  TechJunkGigs</p>
		<p>Email =  example@mail.com</p>
		<p>Phone =  1234567890</p>
	<div>
		<h1 align = "center">
			<a href = "page2.php?name=<?php echo $enc_name;?>
			&email=<?php echo $enc_email;?>&phone=<?php echo $enc_phone;?>">Pass Data</a>
		</h1>

Pass Data carry the data of name, email, and phone to other data and data pass with URL

Here data available in URL but in encoded for with base64_decode() function.

page2.php

In this page, get the value from URL but this data is encoded format so we have to decode this data with base64_decode() function and display.

<?php
	$name = $_GET['name'];
	$email = $_GET['email'];
	$phone = $_GET['phone'];
	$dec_name = base64_decode($name);
	$dec_email = base64_decode($email);
	$dec_phone = base64_decode($phone);
	
	echo $dec_name."<br/>";
	echo $dec_email."<br/>";
	echo $dec_phone."<br/>";
?>

In this page, all the encoded data decode with base64_decode() function and show the accurate data.

You Can Also Check

Ajax Live Data Search using PHP and MySQL

Insert Data into MySQL database with PHP and AJAX without refreshing page

How to Customize a Google Map Custom Markers in PHP

Google Charts in PHP with MySQL Database using Google API

I hope this tutorial helped you to learn Encode and Decode Query String value In PHP. To get the latest news and updates follow us on twitter facebook, subscribe to our YouTube channel. If you have any query then please let us know by using comment.

Categories: PHP
Jamaley Hussain: Hello, I am Jamaley. I did my graduation from StaffordShire University UK . Fortunately, I find myself quite passionate about Computers and Technology.
Related Post