MD5() Encryption in PHP

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

Topic-53
MD5() Encryption in PHP
Part – 2

images-1-300x123-300x1234-300x123

DOC,PDF

-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.

Program:
<?php
if( isset($_POST[‘password’]) && !empty($_POST[‘password’]) ){

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

Output: (If you enter correct password or string)
php53_2-1-o

Output:(If you enter wrong password or string)
php53_2-1-o2

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.

images-1-300x123-300x1234-300x123

DOC,PDF

Leave a Reply