MD5() Encryption in PHP

MD5() Encryption in PHP
MD5() Encryption in PHP

MD5() Encryption in PHP
Part – 2



-In last topic we discussed about the encryption of a string using md5().
-In this topic we are going to see how this function is helpful in encrypting the passwords.
-It increases the security in saving the credentials.

Let us see the following program,
Here we saved the encrypted string “password” in a text file pswd.txt.
We are using form elements,
-A text form to enter the password.
-A submit button to submit the form.
Here we are using text box for our convenience. You can use password attribute to hide the password in form.

if( isset($_POST[‘password’]) && !empty($_POST[‘password’]) ){

$password = md5($_POST[‘password’])
$filename = ‘pswd.txt’;
$pswd = fread($handle,filesize($filename));
if($password == $pswd){
echo “Password Matched”;
echo “Password does not matched”;
echo “Please enter the password!!”;
<form action=”md5.php” method=”post”>
<br/>Password::<br><input type=”text” name=”password” />
<br/><br/><input type=”submit” value=”submit” />

Output: (If you enter correct password or string)

Output:(If you enter wrong password or string)

In the above program we are directing the page to itself,
-When you enter the password and submit the form then the condition will be checked in if blocks and returns true.if block will be executed. Here the string you entered will be encrypted using md5() function and saved into the variable $password.
-Later the file pswd.txt will be opened and the string in the file is saved in $pswd variable.
-The $password and $pswd variables are compared in if block and returns true if both are equal else returns false.
-Here we saved the hash code of string “password” in pswd.txt file. So if you enter “password” in text box then if block will be executed and displays “Password Matched” else it will display “Password does not matched”.
Hence we implemented saving the passwords and retrieving them using md5() function.



Leave a Reply