How to Encrypt/Decrypt id for URL in PHP
- Tech Area
- January 25, 2023
In this tutorial, we have discuss how to hide or encrypt/decrypt id/string for url in php. If you are working on a website in which you have to fetch data from database and that details you want to show on web page with the help of unique id then you want to pass sensitive data using url query parameter then this tutorial is very important for you.
In this tutorial, we have discuss how to pass sensitive data with the help of url query parameter and encrypt that data using hash method to another page.
Source Code
connection.php
<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "college_db";
$connection = mysqli_connect("$server","$username","$password","$database");
if(!$connection)
{
echo("connection terminated");
}
?>
config.php
<?php
function encryptor($action, $string) {
$output = false;
$encrypt_method = "AES-256-CBC";
$secret_key = 'Tech Area';
$secret_iv = 'tech@12345678';
$key = hash('sha256', $secret_key);
$iv = substr(hash('sha256', $secret_iv), 0, 16);
if( $action == 'encrypt' )
{
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
$output = base64_encode($output);
}
else if( $action == 'decrypt' )
{
$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
}
return $output;
}
?>
index.php
<?php
require_once 'connection.php';
require_once 'config.php';
?>
<!DOCTYPE html>
<html>
<head>
<title>Registration Details</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href='http://fonts.googleapis.com/css?family=Pacifico' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 50%;
margin-left: auto;
margin-right: auto;
font-size: 1.6rem;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>
<div class="container">
<center><h1>Encrypt/Decrypt ids/string for URL in PHP</h1></center>
<br>
<table>
<tr>
<th>S.No</th>
<th>Name</th>
<th>Email</th>
<th>Mobile</th>
<th>Action</th>
</tr>
<?php
$fetch_data = mysqli_query($connection,"select id, name, email, mobile from tbl_student");
while($result = mysqli_fetch_array($fetch_data))
{
$id = encryptor('encrypt', $result['id']);
?>
<tr>
<td> <?php echo $result['id']; ?> </td>
<td> <?php echo $result['name']; ?> </td>
<td> <?php echo $result['email']; ?> </td>
<td> <?php echo $result['mobile']; ?> </td>
<td> <a href="show-details.php?id=<?php echo $id; ?>">View Details</a> </td>
</tr>
<?php } ?>
</table>
</div>
</body>
</html>
show-details.php
<?php
require_once 'connection.php';
require_once 'config.php';
?>
<!DOCTYPE html>
<html>
<head>
<title>Registration Details</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href='http://fonts.googleapis.com/css?family=Pacifico' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<center><h1>Encrypt and Decrypt ids/string for URL Using PHP</h1></center>
<?php
if(isset($_GET['id']) && !empty($_GET['id']))
{
$id = $_GET['id'];
$id = encryptor('decrypt',$id);
if(!empty($id))
{
$fetch_data = mysqli_query($connection,"select * from tbl_student where id='$id'");
$result = mysqli_fetch_array($fetch_data);
}
}
?>
<div style="font-size:22px; margin: 5% 36%;">
<h3>Details are given below:</h3>
<p> <b>Name :</b><?php echo $result['name'];?> </p>
<p> <b>Dob:</b> <?php echo $result['dob'];?> </p>
<p> <b>Designation:</b><?php echo $result['designation'];?> </p>
<a href="index.php"><button class="btn btn-primary">Back</button></a>
</div>
</div>
</body>
</html>
Download Source Code
Subscribe us via Email
Join 10,000+ subscriber
PHP Projects
Student record system project in PHP and MySQL
Last Updated: July 3, 2024
Food ordering system project in PHP and MySQL
Last Updated: June 27, 2024
Tourism management system project in PHP and MySQL
Last Updated: June 24, 2024
Restaurant table booking system project in PHP and MySQL
Last Updated: February 22, 2024
Hostel management system project in PHP and MySQL
Last Updated: February 14, 2024
Online Pizza Delivery project in PHP
Last Updated: February 4, 2024
Parking Management System project in PHP
Last Updated: November 5, 2023
Employee Attendance Management System project in PHP MySQL
Last Updated: October 3, 2023
Visitors Management System project in PHP
Last Updated: August 28, 2023
Library Management System project in PHP and MySQL
Last Updated: May 29, 2023
Expense Management System project in PHP and MySQL
Last Updated: May 22, 2023
Hospital Management System project in PHP and MySQL
Last Updated: May 5, 2023
Online quiz with e-certificate in PHP CodeIgniter with MySQL
Last Updated: April 29, 2023
SNIPE – IT Asset management system v6.1.0
Last Updated: April 21, 2023
Online Quiz System project in PHP CodeIgniter with MySQL
Last Updated: February 8, 2023
Employee Management System project in PHP
Last Updated: January 21, 2023