How to reset password using email verification in PHP

0
222
password-reset-code.php


<?php

include('security.php');
//$connection = mysqli_connect("localhost","root","","billing");

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

function send_password_reset($get_name,$get_email,$token)
{

    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->Mailer = "smtp";
    $mail->SMTPDebug  = 1;  
    $mail->SMTPAuth   = TRUE;
    $mail->SMTPSecure = "tls";
    $mail->Port       = 587;
    $mail->Host       = "smtp.gmail.com";
    $mail->Username   = "XXXXX@gmail.com";
    $mail->Password   = "****";
    
    $mail->IsHTML(true);
    $mail->AddAddress($get_email);
    $mail->SetFrom("XXXXX@gmail.com",$get_name);
   
    $mail->Subject = "Password Reset Verification";
    $content = " <h2> You have registered with Glorious web tech </h2>
    <h5> Kindly reset your password with the below link </h5>
    <br></br>
    <a href='http://localhost/gloriouswebtech/billing/password-change.php?token=$token&email=$get_email'> Click Me </a>";
    $mail->MsgHTML($content); 
    if(!$mail->Send()) {
      echo "Error while sending Email.";
      var_dump($mail);
    } else {
      echo "Email sent successfully";
    }
}



if(isset($_POST['passwordreset']))
{
    $email = $_POST['email'];
    $token = md5(rand());

    $check_email = "SELECT email FROM register where email='$email'";
    $check_email_run = mysqli_query($connection,$check_email);

    if(mysqli_num_rows($check_email_run) > 0)
    {
        $row = mysqli_fetch_array($check_email_run);
        $get_name=$row['firstname'];
        $get_email=$row['email'];

        $update_token= "UPDATE register SET verify_token='$token' where email='$get_email' ";
        $update_token_run = mysqli_query($connection,$update_token);

        if($update_token_run)
        {
            send_password_reset("$get_name","$get_email","$token");
            $_SESSION['success'] = "We emailed you the Password reset link";
                header('Location: login.php');  
                exit(0);
        }
        else{

            $_SESSION['success'] = "Something went wrong";
                header('Location: login.php');  
                exit(0);
        }
    }
    else
    {
        $_SESSION['success'] = "No Email Found";
                header('Location: register.php');  
                exit(0);
    }
}
?>
<form action="password-reset-code.php" method="post">
<div class="form-group">

<input name="email" type="email" placeholder="Email" data-pristine-required class="form-control">
</div>
</div>
</div>
<button type="submit" class="submit button" style="margin-top: 15px;" name="passwordreset" >Submit</button>
<div style="margin-top: 40px;">
<a href="register.php">Don't have an account ? Register</a> <br>
<a href="login.php">Remember password ? Sign in instead</a>
</div>

</div>

</form>
https://youtu.be/4C8lJ2JAAP4

LEAVE A REPLY

Please enter your comment!
Please enter your name here