How To Encrypt Password In PHP With Source Code

How To Encrypt Password In PHP uses a hash algorithm to make sure the password is secure. Most of the time, it is used in functions like crypt(), password hash(), and md5() to encrypt password.

About The Project

In this tutorial was developed using PHP, HTML, CSS, JavaScript and MySQL Database, In this simple project you can learn on How To Encrypt and Decrypt Password In PHP Using MD5.

The tutorial How To Encrypt Password In PHP MySQL is very simple php code but most effective because in this we have not store password in string format but we have encrypt password and then after we have store password into our database. So no one can hack password because it was encrypt by using md5() php function.

In this tutorial How To Encrypt Password also includes a Downloadable Source Code for free, just find the downloadable source code below and click to start downloading.

To start creating this tutorial How To Encrypt Password, makes sure that you have sublime or any platform of PHP and MySQL installed in your computer.

Steps On How To Encrypt Password In PHP With Source Code

Time needed: 5 minutes

These are the steps on How To Encrypt Password In PHP With Source Code.

  • Step 1: Create folder.

    First, create a folder from your “xampp” folder under “htdocs” folder and name it.
    encrypt password project folder

  • Step 2: Open folder.

    Second, open “sublime text” and open the project folder you’ve created.
    encrypt password open folder

  • Step 3: Create a php file.

    Third, create the php file under your project folder and name it into “index.php“.
    encrypt password create file

  • Step 4: Open xampp.

    Fourth, open “xampp” and simply click the “start” button of “apache” and “mysql“.
    encrypt password open xampp

  • Step 5: Open browser.

    Fifth, Open a browser and go to URL “http://localhost/phpmyadmin/”.
    encrypt password open phpmyadmin

  • Step 6: Create database.

     Six, click on databases tab and Create database naming “registration_db”.

  • Step 7: create database table.

    Seventh, create a database table and name it to “account” and its fields id(INT,11) AUTO INCRIMENT, email(VARCHAR,30), password(TEXT) and retypepassword(TEXT) on your created database.
    encrypt password create table and fields

  • Step 8: Paste the code given below.

    Final, paste all the codes or the complete source code given below into your php file.

How To Encrypt Password Using PHP?

Here’s the codes on how to encrypt password.

CSS File

<style>
body {
  font-family: Arial, Helvetica, sans-serif;
  background-color: black;
}

* {
  box-sizing: border-box;
}

/* Add padding to containers */
.container {
  padding: 16px;
  background-color: white;
}

/* Full-width input fields */
input[type=text], input[type=password] {
  width: 100%;
  padding: 15px;
  margin: 5px 0 22px 0;
  display: inline-block;
  border: none;
  background: #f1f1f1;
}

input[type=text]:focus, input[type=password]:focus {
  background-color: #ddd;
  outline: none;
}

/* Overwrite default styles of hr */
hr {
  border: 1px solid #f1f1f1;
  margin-bottom: 25px;
}

/* Set a style for the submit button */
.registerbtn {
  background-color: #4CAF50;
  color: white;
  padding: 16px 20px;
  margin: 8px 0;
  border: none;
  cursor: pointer;
  width: 100%;
  opacity: 0.9;
}

.registerbtn:hover {
  opacity: 1;
}

/* Add a blue text color to links */
a {
  color: dodgerblue;
}

/* Set a grey background color and center the text of the "sign in" section */
.signin {
  background-color: #f1f1f1;
  text-align: center;
}
</style>

HTML Form

<body>

<form action="#" method="POST">
  <div class="container">
    <h1>Register</h1>
    <p>Please fill in this form to create an account.</p>
    <hr>

    <label for="email"><b>Email</b></label>
    <input type="text" placeholder="Enter Email" name="email" id="email" required>

    <label for="psw"><b>Password</b></label>
    <input type="password" placeholder="Enter Password" name="psw" id="psw" required>

    <label for="psw-repeat"><b>Repeat Password</b></label>
    <input type="password" placeholder="Repeat Password" name="psw_repeat" id="psw-repeat" required>
    <hr>
    <button type="submit" name="submit" class="registerbtn">Register</button>
  </div>
</form>
</body>

Connection

$db = mysqli_connect('localhost', 'root', '') or
die ('Unable to connect. Check your connection parameters.');
mysqli_select_db($db, 'registration_db' ) or die(mysqli_error($db));

Executing process from HTML Form

<?php
if(isset($_POST['submit'])){
$email = $_POST['email'];
$psw = $_POST['psw'];
$psw_repeat = $_POST['psw_repeat'];

$query = "INSERT INTO `account`(email,password,retypepassword) VALUES ('".$email."',md5('".$psw."'),md5('".$psw_repeat."'))";
mysqli_query($db,$query)or die ('Error in updating Database');
?>
<script type="text/javascript">
      alert("Account Successfully Registered!.");
      window.location = "index.php";
    </script>
    <?php
}
?>

Encrypt Password Using MD5()

$query = "INSERT INTO `account`(email,password,retypepassword) VALUES ('".$email."',md5('".$psw."'),md5('".$psw_repeat."'))";

 

Complete Source Code

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
  font-family: Arial, Helvetica, sans-serif;
  background-color: black;
}

* {
  box-sizing: border-box;
}

/* Add padding to containers */
.container {
  padding: 16px;
  background-color: white;
}

/* Full-width input fields */
input[type=text], input[type=password] {
  width: 100%;
  padding: 15px;
  margin: 5px 0 22px 0;
  display: inline-block;
  border: none;
  background: #f1f1f1;
}

input[type=text]:focus, input[type=password]:focus {
  background-color: #ddd;
  outline: none;
}

/* Overwrite default styles of hr */
hr {
  border: 1px solid #f1f1f1;
  margin-bottom: 25px;
}

/* Set a style for the submit button */
.registerbtn {
  background-color: #4CAF50;
  color: white;
  padding: 16px 20px;
  margin: 8px 0;
  border: none;
  cursor: pointer;
  width: 100%;
  opacity: 0.9;
}

.registerbtn:hover {
  opacity: 1;
}

/* Add a blue text color to links */
a {
  color: dodgerblue;
}

/* Set a grey background color and center the text of the "sign in" section */
.signin {
  background-color: #f1f1f1;
  text-align: center;
}
</style>
</head>
<body>

<form action="#" method="POST">
  <div class="container">
    <h1>Register</h1>
    <p>Please fill in this form to create an account.</p>
    <hr>

    <label for="email"><b>Email</b></label>
    <input type="text" placeholder="Enter Email" name="email" id="email" required>

    <label for="psw"><b>Password</b></label>
    <input type="password" placeholder="Enter Password" name="psw" id="psw" required>

    <label for="psw-repeat"><b>Repeat Password</b></label>
    <input type="password" placeholder="Repeat Password" name="psw_repeat" id="psw-repeat" required>
    <hr>
    <button type="submit" name="submit" class="registerbtn">Register</button>
  </div>
</form>
</body>
</html>

<?php
$db = mysqli_connect('localhost', 'root', '') or
die ('Unable to connect. Check your connection parameters.');
mysqli_select_db($db, 'registration_db' ) or die(mysqli_error($db));

if(isset($_POST['submit'])){
$email = $_POST['email'];
$psw = $_POST['psw'];
$psw_repeat = $_POST['psw_repeat'];

$query = "INSERT INTO `account`(email,password,retypepassword) VALUES ('".$email."',md5('".$psw."'),md5('".$psw_repeat."'))";
mysqli_query($db,$query)or die ('Error in updating Database');
?>
<script type="text/javascript">
      alert("Account Successfully Registered!.");
      window.location = "index.php";
    </script>
    <?php
}
?>

 

Download Source Code below

Anyway, if you want to level up your programming knowledge, especially PHP, try this new article I’ve made for you Best PHP Projects With Source Code Free Download 2021.

Summary

This tutorial will encrypt user password and store that encrypted password in database and when user come for login then user enter his password and when system validate user information then at that time also password has been encrypt and validate user information by using that encrypted password, this is because in database table we have already inserted encrypted password. This for prevent from password hacking.

Inquiries

If you have any questions or suggestions about How To Encrypt Password In PHP With Source Code, please feel free to leave a comment below.

1 thought on “How To Encrypt Password In PHP With Source Code”

Leave a Comment