WhatsApp us

How to Encrypt/Decrypt id for URL in PHP

  • Tech Area
  • Last updated on: January 8, 2026



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 20,000+ subscriber

Subscribe on YouTube

PHP Projects
Matrimonial Portal Project in PHP & MySQL Last Updated: December 22, 2025
Event Management System Project in PHP & MySQL Last Updated: December 6, 2025
Online Shopping System Project in PHP MySQL Last Updated: November 26, 2025
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
Visitors Management System project in PHP Last Updated: August 28, 2023
SNIPE – IT Asset management system v6.1.0 Last Updated: April 21, 2023
Employee Management System project in PHP Last Updated: January 21, 2023