Am creating an application... everything is fine so far. In my registration system have used prepared statement and password hashing and have also try to validate user input in my form fields as well. In order for this system to be completed i need to create a forgot password system which means user can request for new password.
What have done is i have a testing site with all the files, which means i can test if works before adding it to the production site.
With the forgot password have used mysqli once everything is working fine i will then update to prepared, because am still learning prepared statement and doing it this way help me understand so don't judge.
The problem am having with my forgot password is the password is not updating once change. see this screenshot: http://prntscr.com/d5hage
Also as mentioned above have used http://prntscr.com/d5hbg1 in my register and verify in my log-in. But how do used the hashing in my forgot password or how do i update it. In my code below have used md5 which am aware is broken. Please all my coding below.
Reset_Password.php
<?php
// include connection
require_once('include/connection.php');
if(isset($_POST['submit'])){
$user_id = base64_decode($_GET['encrypt']);
$passnew = password_hash($password, $_POST['new_password'], PASSWORD_BCRYPT, array( 'cost' => 12 ) );
$sql = "UPDATE `olami560_test`.`user` SET `password` =? WHERE `user`.`id` =?";
$stmt = $con->prepare($sql);
$stmt->bind_param('si',$passnew, $user_id);
$stmt->execute();
if ($stmt->errno) {
echo "FAILURE!!! " . $stmt->error;
}
else echo "Password Changed Successfully.Click on link to login <a href='http://www.olaskee.co.uk/project/allocation/progress/index.php'>Login</a>{$stmt->affected_rows} rows";
$stmt->close();
}
?>
<form method="post" action="<?php echo $_SERVER['HTTP_REFERER']; ?>" >
<label>New Password</label>
<input type="password" name="new_password"/>
<input type="submit" name="submit" value="Reset" />
</form>
forgot_password.php
<?php
// include connection
require_once('include/connection.php');
if(isset($_GET) && !empty($_GET['email'])){
$email = mysqli_real_escape_string($con,$_GET['email']);
$query = "SELECT id
FROM `user`
WHERE `user_name` LIKE '".$email."'
OR `email` LIKE '".$email."'";
$result = mysqli_query($con,$query);
$Results = mysqli_fetch_array($result);
if(count($Results)>=1)
{
$query2 = "SELECT email
FROM `user`
WHERE `user_name` LIKE '".$email."'
OR `email` LIKE '".$email."'";
$result2 = mysqli_query($con,$query2);
$emailvalue = mysqli_fetch_array($result2);
//$token = md5(uniqid(rand(),true));
//$encrypt = md5($Results['id']);
$encrypt = base64_encode($Results['id']);
$message = "Your password reset link send to your e-mail address.";
$to = $emailvalue['email'];
$subject="Forget Password";
$from = '[email protected]';
$body= 'Hi, <br/> User <br/>You Requested for Reset Password. <br><br>http://www.olaskee.co.uk/project/allocation/tms/reset_password.php?token='.$token.'&encrypt='.$encrypt.'&action=reset<br/> <br/>--<br>.olaskee<br>';
$headers = "From: " . strip_tags($from) . "\r\n";
$headers .= "Reply-To: ". strip_tags($from) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to,$subject,$body,$headers);
echo $message;
}
else
{
$message = "Account not found please signup now!!";
echo $message;
}
}
?>
I hope have provide enough explanation for you to understand. Thanks any input.
ok, looking through the code there are a few things I think you need to look at.
On the form change this
<form method="post" action="<?php echo $_SERVER['HTTP_REFERER']; ?>" >
to
<form method="post" action="" >
This should submit the form to itself.
The hashing really needs to be password_hash()
use the following and it will get you started
$passnew = password_hash( $password, $_POST['new_password'], PASSWORD_BCRYPT, array( 'cost' => 12 ) );
On the form for resetting the password it is a good idea to have the user input the new password twice, that way you can check if they have repeated the password correctly.
if( $_POST[ 'pass1' ] == $_POST[ 'pass2' ] ) // Process else error
In your forgot_password.php file you are calling the same sql statement twice. Call it once, check if the row count is greater then one, if it is use the data from within the result, no need to call it again to do the same thing.
Hopefully this will get you going, have a good day.