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

Subscribe on YouTube