I have created a login form for my admin panel, I have the users created stored in a mysql database however all passwords are visible in plain text. I know i need to use md5 to encode the passwords and make them more secure I am just unsure as to how to do this...
My code is below:
<?php
session_start();
include("../config.php");
$username = trim($_GET['username']);
$password = trim($_GET['password']);
$cpassword = trim($_GET['cpassword']);
$name = trim($_GET['name']);
$email = trim($_GET['email']);
//Server side validation
//check if all fields are enter or not
if($username == '' || $password == '' || $name =='' || $email =='')
{
$output['error'] = 'error';
$output['msg'] = 'All fields are mandatory';
}
//Check password and confirm password match or not
else if($cpassword != $password)
{
$output['error'] = 'error';
$output['msg'] = 'Password and confirm password do not Match';
}
else if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$output['error'] = 'error';
$output['msg'] = 'Enter correct Email ID';
}
// Insert the data into the database
else{
// SELECT MATCH FROM THE DATABASE
$queryMatch = "SELECT * FROM `users` where username=?";
$statementMatch = $db->prepare($queryMatch);
$statementMatch->execute(array($username));
if($statementMatch->rowCount() > 0) {
$output['error'] = 'error';
$output['msg'] = 'Username Already exists.Try another username.';
}else{
$query = "INSERT INTO `users` SET username=? , password =? , name = ? ,email=?";
$parameters = array($username,$password,$name,$email);
$statement = $db->prepare($query);
$statement->execute($parameters);
$output['error'] = 'success';
$output['msg'] = 'Registered Successfully.Redirecting to Login Page..';
}
}
echo json_encode($output);
?>
If anyone could help me with this it'd be great, as I say I know I need to use md5 at some point I am just unsure as how I would add it to this code?
EDIT::
I want to know how to add md5 to this code so that passwords are encoded with md5 and saved in a secure manner. BUT I also want to know how to make passwords more secure. Currently new users have to have a password of 8 characters or more and contain one number and a symbol but ideally I could do with a way to have them generate 100/100 secure passwords.
If md5 isn't as secure as it seems what other options do I have that can be used instead of md5?
EDIT::
I now realise md5 isn't as secure as initially thought. Please refer to @kyborek's answer for the best solution to this issue.