Ok, so I am sending a link to a user with parameters in the URL like so:
mydomain.com/home/[email protected]&token=85988817a21062f92016a7065c
The user clicks on the link and is taken to reset-password.php. On this page there are 3 fields:
Password
Confirm Password
Math Captcha
I want to update the database with the new password the user submits based on the parameters in the URL. The problem i'm running into is, it is not updating the database with the new password.
Here is my reset-password.php code:
function checkDataValidity(){
if(empty($_POST['password'])){
throw new Exception("Please enter a password.");
}
}
function updateUserPassword($dbHandler) {
try {
$salt = dechex(mt_rand(0, 2147483647)) . dechex(mt_rand(0, 2147483647));
$password = hash('sha256', $_POST['password'] . $salt);
for($round = 0; $round < 65536; $round++){
$password = hash('sha256', $password . $salt);
}
$urlEmail = isset($_GET['email']) ? $_GET['email'] : '';
$urlToken = isset($_GET['token']) ? $_GET['token'] : '';
$query_params = array(
':password' => $password,
':salt' => $salt,
':emptyToken' => '',
':user' => $urlEmail,
':token' => $urlToken
);
$dbHandler->beginTransaction();
$sql = "UPDATE users SET password = :password, salt = :salt, recovery = :emptyToken WHERE username = :user AND recovery = :token";
$stmt = $dbHandler->prepare($sql);
$result = $stmt->execute($query_params);
$dbHandler->commit();
} catch(Exception $dbException){
$dbHandler->rollback();
echo 'The following error occured: <br/>'.$dbException->getMessage();
}
return true;
}
require("config.php"); //connects to the database
if(!empty($_POST)){
try {
checkDataValidity();
$updatePassword = updateUserPassword($db);
if($updatePassword){
$message = "Success!";
} else {
$message = "Please try again.";
}
} catch (Exception $e){
echo 'The following error occured: <br/>'.$e->getMessage();
}
}
I am not receiving any errors anymore, but I know it has somethins to do with this:
$urlEmail = isset($_GET['email']) ? $_GET['email'] : '';
$urlToken = isset($_GET['token']) ? $_GET['token'] : '';
If I replace this isset($_GET['email']) ? $_GET['email'] : '' with the actual email address in the database, everything works fine. Same thing with token.
So basically what I am trying to do is GET the email and token parameter out of the URL and use that in my sql query as seen above. So when the user submits their new password update the existing password for that user and remove the token from the DB.
Is there something wrong with my code above to GET the parameters in the URL and use that in my query?
$_GETwithecho "<pre>".print_r($_GET,true)."</pre>";.$_POSTfor everything.password_hash().updateUserPassword($db)beupdateUserPassword($dbHandler)- I didn't see anything assigned to$db.$_GETand I see$_POST. Wondering if your form's action is get/post, you might want to change your$_GETs to$_REQUEST. At this point, I don't know what else to say that will be of any further help.