I have created a registration system for a sample android app. I can both register and login without any problems. The last part is the forgot password one. Here is my logic. When the user forgets his password,he sends his email to my server and in return he gets an email back telling him to visit a link. That link takes him to a page where he can enter and confirm his new password. For some reason though the password is not updated.
So I will post my code here.
register.php
<?php
session_start();
require "init.php";
header('Content-type: application/json');
$id = $_POST['id'];
$email = $_POST['email'];
$user_name = $_POST['user_name'];
$user_pass = $_POST['user_pass'];
$passwordEncrypted = sha1($user_pass);
$confirmPass = $_POST['confirm_pass'];
$confPasswordEncrypted = sha1($confirmPass);
$msg = "Congratulations. You are now registered to the most amazing
app ever!";
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$don = array('result' =>"fail","message"=>"Please enter a valid email");
}
if($email && $user_name && $user_pass && $confirmPass && filter_var($email, FILTER_VALIDATE_EMAIL)){
$sql_query = "select * from user_info WHERE email ='".mysqli_real_escape_string($con, $email)."' or user_name
='".mysqli_real_escape_string($con, $user_name)."'";
$result = mysqli_query($con, $sql_query);
$results = mysqli_num_rows($result);
if ($results){
$don = array('result' =>"fail","message"=>"Email or username exists.");
}else{
$sql_query = "insert into user_info values('$id','$email','$user_name','$passwordEncrypted','$confPasswordEncrypted');";
if(mysqli_query($con,$sql_query)){
$don = array('result' =>"success","message"=>"Successfully registered!Well done");
mail($email,"Well done. You are registered to my sample app!",$msg);
$_SESSION['id'] = mysqli_insert_id($con);
}
}
}else if(!$email){
$don = array('result' =>"fail","message"=>"Please enter a valid email");
}else if(!$user_name){
$don = array('result' =>"fail","message"=>"Please enter your username");
}else if(!$user_pass){
$don = array('result' =>"fail","message"=>"Please enter a password");
}else if(!confirmPass){
$don = array('result' =>"fail","message"=>"Please confirm your password");
}
echo json_encode($don);
?>
And the changepassword.php
<?php
require "../init.php";
session_start();
if(isset($_POST['update'])){
$password = $_POST['user_pass'];
$confpassword = $_POST['confirm_pass'];
if($password !== $confpassword){
echo "Passwords don't match!";
}else{
$id = $_SESSION['id'];
if(mysqli_query($con,"UPDATE user_info SET
user_pass='$password',confirm_pass = '$confpassword' WHERE id='$id'")){
echo "Password successfully changed!!!";
}
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="css/login-css.css" media="all"/>
</head>
<body>
<div class="updatepass">
<h1>Update Password</h1>
<form action="" method="post">
<input type="password" name="user_pass" placeholder="Password" required="required" />
<input type="password" name="confirm_pass" placeholder="Confirm Password" required="required" />
<button type="submit" class="btn btn-primary btn-block btn-large" name="update">Update</button>
</form>
</div>
</body>
</html>
Any ideas on how to update the password?
Thanks.