Programming | Android | Tips N Tricks

Post Page Advertisement [Top]

What is MD5 ?
The md5() function calculates the MD5 hash of a string.
The md5() function uses the RSA Data Security, Inc. MD5 Message-Digest Algorithm.

How MD5() function works ?
When You Pass String as argument to the function md5() it will returns the calculated MD5 hash on success, or FALSE on failure. 

Syntax:  string md5(string,raw)

Now the Question is how we can use to secure our password that when unauthorized person see database entry about login but see can not view real password string only he/she view is hash string of that string.

I have an simple example that will give you explanation about how to use md5() function for your login page in php.

(note that for explanation purpose i have made only two fields User name and Password )

Look at database :
*Table name : "formd5"


*Register.php


<?php
$conn=mysqli_connect("localhost","root","","dbname") or die("Database connection failed!");
?>
<form action="#" method="post">
Register user<br><hr>
User name :<input type="text" name="user"><br>
Password :<input type="text" name="password"><br>
<input type="submit" name="reg" value="Register User">
<a href="login.php">Go to login </a>
</form>
<?php
if(isset($_POST['reg']))
{
$user = $_POST['user'];
$password = md5($_POST['password']); //calculate md hase and stores into variable
$q="insert into formd5 values('','$user','$password')";
mysqli_query($conn,$q) or die("Record not inserted !");
echo "User Registered Succesfully !";
}
?>

*Login.php

<?php
$conn=mysqli_connect("localhost","root","","dbname") or die("Database connection failed!");
?>
<form action="#" method="post">
Login <br><hr>
User name :<input type="text" name="user"><br>
Password :<input type="text" name="password"><br>
<input type="submit" name="log" value="Log In">
<a href="register.php">Go to Register </a>
</form>
<?php
if(isset($_POST['log']))
{
$user = $_POST['user'];
$password = md5($_POST['password']);
//echo $password."<br>";
$q="select * from formd5 where user='$user' and password = '$password'";
$result=mysqli_query($conn,$q) or die("Record not find quey error !");
if(mysqli_num_rows($result) > 0)
{
echo "User Logged in !";
}
else{
echo "password and user name not matched !";
}
}
?>

* when you register using above code will find your database entry about look like this



Your actual Password is secure now. Md5() is very use full and very easy you can learn from giving example if you have any query comment below i will answer each and every Codding if fun so enjoy codding.

Thank you Reader i hope it will help you.


No comments:

Post a Comment

Comments

Bottom Ad [Post Page]

| Designed by Colorlib