0

I'm new to coding and I'm trying to make a reset password form. User would fill up the form with their username, which would connect to clients.json file, find the user and send them new email which would be generated. Somehow it doesn't work and I can't figure out why. I'm sending the code so please let me know if you spot anything or if you have any tips on how to do it better. thanks!

<?php require_once 'top.php';

session_start();
$sUser = file_get_contents('../data/clients.json');
$jUser = json_decode($sUser);

$sForgotUserName = $_POST['txtForgotUserName'];
$sGeneratedPassword = rand(100000, 999999);

$jUser->data->{$sForgotUserName}->password = $sGeneratedPassword;

$sEmail = $jData->data->$sForgotUserName->email;

$sData = json_encode($jData);
    if ($sData == null) {
        sendResponse(0, __LINE__);
    }
    file_put_contents('../data/clients.json', $sData);


$from = "PETRABANK";
    $to = $sEmail;
    $subject = "Your New Password";
    $message = "Please use this password and then change it";
    $headers = "From:" . $from;
    mail($to, $subject, $message, $headers);
    echo "The email message was sent.";

    ?>

<form id="frmForgotPassword" action="forgotpassword.php" method="POST">
    <input name="txtForgotUserName" id="txtUserName" type="text" placeholder="Your username">
    <button>Send new password</button>
</form>

<?php require_once 'bottom.php'?>

clients.json file

{
    "data": {
        "daniel": {
            "name": "dani",
            "lastName": "sss",
            "email": "s@hotmail.com",
            "password": "12345"
        }
    }
}
  • Hello, After you decode json its been converted to array so that you can do like this replace the line `$jUser->data->{$sForgotUserName}->password = $sGeneratedPassword;` to `$jUser['data']['$sForgotUserName']['password'] = $sGeneratedPassword;` same as for email also and then after dump your `$jData` if your new credentials set to the array you have done you job. – Ritesh Khatri Feb 27 '19 at 07:40
  • Hi @Rits . So I used `$jUser['data']['$sForgotUserName']['password'] = $sGeneratedPassword; $sEmail = ['$jUser']['data']['sForgotUserName']['email'];` but still does not work :/ – Danko Jimenez Feb 27 '19 at 07:51
  • have you dump after setting both values? – Ritesh Khatri Feb 27 '19 at 07:51
  • @Rits thanks for your help! Does dump mean to encode back? Well, I did `$jUser['data']['$sForgotUserName']['password'] = $sGeneratedPassword; $sEmail = ['$jUser']['data']['sForgotUserName']['email']; //$jUser->data->{$sForgotUserName}->password = $sGeneratedPassword; //$sEmail = $jData->data->$sForgotUserName->email; $sData = json_encode($jData); if ($sData == null) { sendResponse(0, __LINE__); } file_put_contents('../data/clients.json', $sData);` – Danko Jimenez Feb 27 '19 at 08:00
  • Ah I can see that I used sUser and jUser while decoding and sDaa and jData while encoding. could that be the problem? I will fix it and see. – Danko Jimenez Feb 27 '19 at 08:02
  • No no i mean just print it after setting those values by `print_r()`. if the array contains your updates values then its done. – Ritesh Khatri Feb 27 '19 at 08:02
  • Hasn't done the job :/ – Danko Jimenez Feb 27 '19 at 08:03
  • change this to`$jData->data->$sForgotUserName->email` to `$jUser->data->$sForgotUserName->email` and please provide the output over here. – Ritesh Khatri Feb 27 '19 at 08:05
  • @Rits it worked with password! But doesn't display the email – Danko Jimenez Feb 27 '19 at 08:07
  • so you wanted to update the email also with the new email? – Ritesh Khatri Feb 27 '19 at 08:08
  • @Rits I just wanted togenerate new email $sGeneratedPassword and send it to the email of user. Also, this password should replace his previous password in clients.json – Danko Jimenez Feb 27 '19 at 08:14
  • then you can get that email by `$jUser['data'][$sForgotUserName]['email']` here you can get the email and send mail to that email. – Ritesh Khatri Feb 27 '19 at 08:55

0 Answers0