21
Step - 1 - forgot_password.php
<?php include('../Class/config.php'); ?>
<?php
if (isset($_POST['reset']))
{
unset($_POST["reset"]);
// Check if email exists
$Uni->where('user_email',$_POST['user_email']);
$user = $Uni->getOne('user');
if ($Uni->count > 0)
{
// Generate a unique token
$token = bin2hex(random_bytes(16));
$created_at = date('Y-m-d H:i:s', strtotime('+1 hour'));
$_POST["user_email"] = htmlspecialchars($_POST["user_email"], true);
$_POST["created_at"] = $created_at;
$_POST["token"] = $token;
$b = $Uni->insert('password_resets',$_POST);
// Send reset email
echo $resetLink = BASE_URL."seller/reset_password.php?token=$token";
// $subject = 'Password Reset Request';
// $message = "To reset your password, please click the following link: $resetLink";
// $headers = 'From: no-reply@yourdomain.com';
// mail($email, $subject, $message, $headers);
$msg_sussess = 'A password reset link has been sent to your email.';
}
else
{
$msg_danger = 'Email address not found.';
}
}
?>
<form method="POST" class="authentication-form">
<div class="mb-3">
<label for="exampleInputEmail1" class="form-label">Email <small>*</small></label>
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Email" name="user_email" />
</div>
<div class="mb-0 text-center pt-3 d-grid">
<button class="btn btn-primary" name="reset" type="submit">Submit</button>
</div>
</form>
Step - 2 - reset_password.php
<?php include('../Class/config.php'); ?>
<?php
// reset_password.php
if (isset($_POST['reset_password']))
{
unset($_POST['reset_password']);
$token = $_POST['token'];
$newPassword = $_POST['pass'];
// $Uni->where('created_at', NOW() - INTERVAL 1 HOUR); // created_at > NOW() - INTERVAL 1 HOUR
$Uni->where('token',$token);
$reset_user = $Uni->getOne('password_resets');
if ($Uni->count > 0)
{
$Uni->where('user_email', $reset_user['user_email']);
$user = $Uni->getOne('user');
if ($Uni->count > 0)
{
// Update the password
$update = array('pass' => $newPassword);
$Uni->where('user_email', $reset_user['user_email']);
$Uni->update('user', $update);
// Delete the reset token
$Uni->where('token', $reset_user['token']);
$Uni->delete('password_resets');
$msg_sussess = 'Your password has been updated.';
}
else
{
$msg_danger = 'User not found.';
}
}
else
{
$msg_danger = 'Invalid or expired token.';
}
}
?>
<form method="POST" class="authentication-form">
<div class="mb-3">
<input type="text" name="token" class="form-control" value="<?php echo htmlspecialchars($_GET['token']); ?>">
<input type="password" class="form-control" name="pass" placeholder="New password" required>
</div>
<div class="mb-0 text-center pt-3 d-grid">
<button class="btn btn-primary" name="reset_password" type="submit">Reset</button>
</div>
</form>