1 Star2 Stars3 Stars4 Stars5 Stars (11 votes, average: 5.00 out of 5)

PHP DATA ENCRYPTION INSERT RETRIEVE MYSQL DATABASE

PHP Data Encryption is important to safety and privacy. In this lesson, I am going to show you how to insert and retrieve encrypted data in PHP using a MySQL database. Using these methods will make your websites and applications more secure.

PHP DATA ENCRYPTION INSERT RETRIEVE MYSQL DATABASE

In an earlier PHP Data Encryption lesson, I showed you guys how to  use OpenSSL to encrypt and decrypt your data. In this lesson, I will show you how to store it and retrieve it from a MySQL Database.

Tip: Click on images to make them larger. If you find anything wrong on this page or need further assistance please comment below or contact me. 🙂

Code Example

function encryptthis($data, $key) {
$encryption_key = base64_decode($key);
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
$encrypted = openssl_encrypt($data, 'aes-256-cbc', $encryption_key, 0, $iv);
return base64_encode($encrypted . '::' . $iv);
}

function decryptthis($data, $key) {
$encryption_key = base64_decode($key);
list($encrypted_data, $iv) = array_pad(explode('::', base64_decode($data), 2),2,null);
return openssl_decrypt($encrypted_data, 'aes-256-cbc', $encryption_key, 0, $iv);
}

Create Database For Encryption

Create a database to insert your encrypted data.  Name the database “users” in your phpmyadmin dashboard. Then create a table called people. Next, make 4 columns in the table id, name, email, reg_date. Set the name and email fields to TEXT. Do not use varchar because varchar will only give you 255 characters and you may need more than that for your encrypted code. We will use this database to insert and retrieve the data.

CREATE TABLE `people` (
`id` int(11) NOT NULL,
`name` text NOT NULL,
`email` text NOT NULL,
`reg_date` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Create A Config File

You need to create a config file to connect to your database. This will let us insert our encrypted data and retrieve our encrypted data.

Subscribe To My YouTube Channel:

Inserting PHP Encrypted Data

To insert your PHP Encrypted Data into the database you created from the information above, use this code.

mysqli_query($con,"INSERT INTO people(`name`, `email`)VALUES ('$name','$email')");
echo '<div class="alert alert-success">You entered successfully</div>';

Header.php File

<?php
date_default_timezone_set('America/New_York');
include_once('functions.php');
?>
<html>
<head>
<title>PHP ENCRYPTION DECRYPTION MYSQL</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" type="text/css" >
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css">
</head>
<body>
<div class="container">

Footer.php File

[</div>
</body>
</html>
<?php
$con->close();
?>/code]

Copy all the files here and set them up on your server. Follow the video instructions and if you have any questions, ask them below.

PHP DATA ENCRYPTION INSERT RETRIEVE MYSQL DATABASE was last modified: January 11th, 2021 by Maximus Mccullough

Please like, share and subscribe.

Advanced Programming Made Easy

I hope you enjoyed the article. If I was able to help you please consider a tip for the content.

One Time Tip

more tips

Reoccuring Tips

Tips

Cool People Share:
PHP DATA ENCRYPTION INSERT RETRIEVE MYSQL DATABASE

14 Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.