How to use md5 hash in php – what is hash?

Spread the love

How to use md5 hash in php: When we create a website with an account system, and a user can login to his account with his username and password, the password and username must be saved in the website’s database.

In this case, the user’s password is a very sensitive and important thing that the user’s account will not be protected unless it is saved properly. Many of my friends have a question, when we create an account on a certain website, can the owner or company of the website see our password?

So friends here I keep saying, since the owner of the website or the company that owns the website they can see all of us. But they never do anything bad with our data. There are some rogue individuals or companies that do this kind of work. So before making any kind of account of any website, it is better to know the details about that website

MD5 and HASH

Also Read: php Registration Form with Database Code – Signup Form in php

Friends we will learn in today’s article and learn how to use hash and md5 code for password? We will also find out why no one can see the passwords of any company or any owned website.

At present, all types of websites are creating passwords using this md5 and hash on account system websites. Which is very complex and difficult. If you are a computer science student then this article will be 100% useful for your seminar project. Be sure to stay with us and read the whole article.

What is the md5 in php?

MD5 hash algorithm is a very powerful algorithm that calculates md5 hash. MD5 (Message-Digest Algorithm) takes a message of arbitrary length as input and generates a 128-bit message digest of the input.

A MD5 hash of 16 or 32-character hexadecimal numbers of a particular string. This passed string will be converted to an encrypted hexadecimal form. This md5 code is currently widely used to secure various digital things. Today through this article we will learn how md5 code works on user registration and login page in php.

How md5 work in php?

We can better understand how to add md5 password by looking at this code below. Friends, the password of that website is encoded using md5 code. When we create an account for those websites, the password we set there is saved in their database as follows.

<h3>Encrypted Password Result</h3>
Password is :- 1a<br>
<?php
$password = '1a';
$encrypted_password_result = md5($password );
echo"Encrypted Password is:- ";
echo $encrypted_password_result;
?>

<br><br>
OR


<h3>Encrypted Password Result</h3>
Password is :- 1a<br>
<?php
$password = '1a';
echo"Encrypted Password is:- ";
echo md5($password );
?>

There is a problem with encoding the password using this md5 code. This can be understood by looking at the results below. If a password is encrypted with the md5 code, then the encrypted number of the password will be the same every time it is used.

md5hash

This is how we can encrypt the password with md5 for registration. The encrypted password will be saved in the database on the website. And You can use md5 encrypted password code in PHP with login page

<?php
$username = $_POST['username'];
$password = $_POST['password'];

$encrypted_password = md5($password);

echo $encrypted_password; // you can use it as per your requirement
?>

This is why all account system websites have a set of rules for giving passwords where you are asked to create a password with different letters, special, characters, numbers etc. Like – Password@a`8^%

What is the Hash Code in php?

There is a problem with encoding the password using this md5 code. This can be understood by looking at the results below. If a password is encrypted with the md5 code, then the encrypted number of the password will be the same every time it is used. The hash code is used to solve this problem of encrypted output of md5 coding. How to use md5 and hash in php.

Hash code is an encrypted algorithm of 60 characters which is very Powerful and complex. The hash code is used to solve this problem of encrypted output of md5 coding. Which is currently the most powerful encrypted system.

How Hash code work in php?

When an input data is encrypted with a hash code code, the input hash code returns 60 characters encrypted output. The most important thing here is, If I set the input value to the same and every time I reload the page to take the output, the 60 character hash code encrypted output will change after every successful loading.

<h3>HASH Encrypted Password Result</h3>
Password is :- 1a<br>
<?php
$password = '1a';
echo"<br>";
$encrypted_password = password_hash($password, PASSWORD_DEFAULT);
echo$encrypted_password;
?>
 
<br><br>
<h3>HASH Encrypted Password Result</h3>
Password is :- 1a<br> 
<?php
$password = '1a';
echo"<br>";
echo password_hash($password, PASSWORD_DEFAULT);
?>

Also Read: php session Code not Working on Live Server – error Fix

Below is an output of the result of this change

How to use md5 and hash in php
How to use md5 and hash in php

The hash code can be used in this way to be used on the registration page –

<?php
$username = $_POST['username'];
$password = $_POST['password'];

$encrypted_password = password_hash($password, PASSWORD_DEFAULT)

echo $encrypted_password; // you can use it as per your requirement
?>

But to use the hash code on the login page, you have to do a little different coding which is used as follows

<?php
$username = $_POST['username'];
$password = $_POST['password'];

$sql = ("SELECT * FROM register WHERE username ='$username '");
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) 
    {	
		$dbusername = $row['username '];

		$hash_from_database = $row['password'];
    }

if (!password_verify($login_password, $hash_from_database)) 
{
	echo"<font color='red'>You are enter Wrong Password</font>";
	exit; 
}
?>

Also Read: Best Web Hosting for WordPress Cheap with Great Performance

I would say this hash code encrypted password account system is very good for website and powerful encrypted password will be created for your users. Breaking this encrypted password would be very impossible for any rogue person. How to use md5 and hash in php.

Question answer

Many of my friends have a question, when we create an account on a certain website, can the owner or company of the website see our password?

So friend any website owner can see your username and password, but not understand your password. because your password is encrypted with hash coding but the website owner can be changed your password manually if he/she want.


Spread the love

Leave a Comment