Hello, i want to allow my users to reset their passwords if they forget it.
My problem is that i lose the code sent to user via email, if this is the link sent to user via email
http://localhost/citi/forgot.php?code=8b15388bc5ca7e2821ab6ce59b465f684942dd7901f4e31439575ab72028f84f
, when the user get to forgot.php and type in the new password, i need to update user account based on this code
code=8b15388bc5ca7e2821ab6ce59b465f684942dd7901f4e31439575ab72028f84f
.
But when i press the submit button, i lose this code
code=8b15388bc5ca7e2821ab6ce59b465f684942dd7901f4e31439575ab72028f84f
and my update fails.
I tryed putting it in a
$check_cod=mysql_real_escape_string($_GET['code']);
$check_code=$_SESSION['$check_cod'];
but it is still not working.
here is my full code.
if(!empty($_GET['code']) && isset($_GET['code']))
{
$code=mysql_real_escape_string($_GET['code']);
$check_cod=mysql_real_escape_string($_GET['code']);
$check_code=$_SESSION['$check_cod'];
$c=mysqli_query($conn,"SELECT forgetId FROM forget WHERE forgetcode='$code'");
if(mysqli_num_rows($c) > 0)
{
$count_forgetId=mysqli_query($conn,"SELECT forgetId FROM forget WHERE forgetcode='$code' and statusTwo='0'");
if(mysqli_num_rows($count_forgetId) == 1)
{
mysqli_query($conn,"UPDATE forget SET statusTwo='1' WHERE forgetcode='$code'");
}
else
{
$msg ="<h2>You have already reset your password, to reset your password again, <a href='forgot.php'>please click here</a></h2>";
}
}
else
{
$msg ="Wrong activation code.";
}
}
////////////////////////////////////////////////////////////////////////////////
if($_SERVER["REQUEST_METHOD"] == "POST"){
//define variables and set to empty values
$passwordErr = $passwordconfirmErr = $passwordQErr = $passwordlenghtErr = "";
$password = $passwordconfirm = "";
if (empty($_POST["password"]) OR empty($_POST["passwordconfirm"])) {
$passwordErr = "Password is required";
} else {
$password = test_input($_POST["password"]);
}
if (empty($_POST["passwordconfirm"])) {
$passwordconfirmErr = "Confirm password is required";
} else {
$passwordconfirm = test_input($_POST["passwordconfirm"]);
}
//Check to make sure password and confirm password match in lenght
if (strlen($_POST["passwordconfirm"]) < 6){
$passwordlenghtErr = "Password must be more than 6 characters.";
} else {
$passwordconfirm = test_input($_POST["passwordconfirm"]);
}
if($_POST['password'] != $_POST['passwordconfirm']) {
$passwordQErr = "Passwords do not match";
} else {
$passwordconfirm = test_input($_POST["passwordconfirm"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$msg='';
//die($check_code);
if(!empty($_POST['password']) && isset($_POST['password'])){
$password=mysql_real_escape_string($_POST['password']);
$salt = uniqid(mt_rand()+microtime(true), true);
$hpassword=hash('sha256',$password.$salt); // Encrypted password
//$check_code=mysql_real_escape_string($_GET['code']);
$count_uid = "SELECT `uid` FROM `users` WHERE forgetcode='$check_code'";
die($count_uid);
$count=mysqli_query($conn, $count_uid);
// check code
if(mysqli_num_rows($count) == 1)
{
$upd = "UPDATE `users` SET password='$hpassword' WHERE forgetcode='$check_code'";
mysqli_query($conn,$upd);
}
}