Simple Password Safety using MD5 Encryption in PHP

In this tutorial we will create a simple safety password using MD5 encryption. PHP offers the md5 function which calculates the MD5 hash of a string using the MD5 Message Digest Algorithm. This algorithm takes a string and generates a 128-bit fingerprint of the input string. MD5 is a one-way encryption which means that you cannot decipher the fingerprint to get the original string. Yet another feature of MD5 is that the algorithm will always generate the same fingerprint for a given string. This tutorial demonstrates how to use PHP md5 function to encrypt the passwords in your website. This simple project compose of two html pages and two PHP file and has a text(.txt) file for the storing the encrypted password.

Sample Code

PHP Script for Registration
  1. <!DOCTYPE html>
  2. <body>
  3. <table>
  4.         <tr>
  5.                 <td><a href="user_login.php">Home</a></td>
  6.         </tr>
  7. </table>
  8. </body>
  9. <?php
  10. foreach(file('password.txt') as $line) {
  11.     if(empty($line)) continue;
  12.     $lineArray = explode(':', $line);
  13.     $username = rtrim($lineArray[0]);
  14.  
  15.         if ($_POST["username"] == $username){
  16.         echo "This user already exist.";
  17.         return;
  18.     }
  19. }
  20. $line = $_POST["username"] . ":" . md5($_POST["password"]) . "\n";
  21. file_put_contents('password.txt', $line, FILE_APPEND);
  22. echo "Registration completed successfully.";
  23. ?>
HTML Script Form for Registration User Registration

User Registration Form

Sample Image Result For more tutorials just visit this website @www.sourcecodester.com and don't forget to Like and Share. Enjoy Coding.

Add new comment