Skip to main content

How to Get Forgot Password and Reset Password Using PHP OOP Concept using PDO for Database

 

How to Get Forgot Password and Reset Password Using PHP OOP Concept and PDO for Database



forgot password and reset password






Today I will explain how to Forgot password and Reset password using PHP and OOP concept and PDO database, in this tutorial i will implement.
Before moving towards the tutorial we need first User Account Activation using email Verification using Php  user registration and login script in php, so if you  do first that same Gmail verification process on that tutorial also  Its is a very important feature on membership website is password and reset system because some users are forget their password very quickly.

This Article demonstrate to build an get forgot password and reset password .This is a continue tutorial and previous tutorial is how to verify email on Gmail account using PHP and PDO for database read before this tutorial 


 and then know we are create two empty PHP file named as

1.forgotpassword.php
2.resetpassword.php

1.forgotpassword.php

As a developer your priority is given to this process of forget password because of users something spend much time to reset the password and  forgot it . and they try to recovery .


<?php
session_start();
require_once 'user.php';
$user = new USER();

if($user->is_logged_in()!="")
{
 $user->redirect('home.php');
}

if(isset($_POST['submit']))
{
 $email = $_POST['email'];
 $stmt = $user->query("SELECT userID FROM users WHERE userEmail=:email LIMIT 1");
 $stmt->execute(array(":email"=>$email));
 $row = $stmt->fetch(PDO::FETCH_ASSOC); 
 if($stmt->rowCount() == 1)
 {
  $id = base64_encode($row['userID']);
  $code = md5(uniqid(rand()));
  
  $stmt = $user->query("UPDATE tbl_users SET mdCode=:code WHERE userEmail=:email");
  $stmt->execute(array(":code"=>$code,"email"=>$email));
  
  $message= "
       Hello , $email
       <br /><br />
       Click Following Link To Reset Your Password 
       <br /><br />
       <a href='http://localhost/testsignup1.php/resetpassword.php?userid=$id&mdcode=$code'>
click here to reset your password</a>
       <br /><br />
       thank you :)
       ";
  $subject = "password reset";
   $user->send_mail($email,$message,$subject);
   $msg = " We have sent an email to $email.Please click on the password reset link in the email to generate new password.";
 }
 else
 {
  $msg = "<strong>Sorry!</strong>  this email not found. ";
 }
}
?>

<!DOCTYPE html>
<html>
  <head>
    <title>Forgot Password</title>
  </head>
  <body id="login">
    <div class="container">
          <form  method="post">
        <h2>Forgot Password</h2><hr />
         <?php
   if(isset($msg)) {echo $msg;}
   else
   {
    ?>
               <div>
    Please enter your email address. You will receive a link to create a new password via email.!
    </div>  
                <?php
   }
   ?>
        <input type="email" placeholder="Email address" name="email" required />
      <hr />
        <button  type="submit" name="submit">Generate new Password</button>
      </form>

  </body>
</html>

In this step we get the user entered email and check if user exist in database or not but before check always validate user entered data or you can check our email .And if the user exist in our database then we send hashed email and password on link to his registered email id you can check send mail using php

2.resetpassword.php


<?php
require_once 'user.php';
$user = new USER();

if(empty($_GET['userID']) && empty($_GET['mdcode']))
{
 $user->redirect('index.php');
}

if(isset($_GET['userID']) && isset($_GET['mdcode']))
{
 $id = base64_decode($_GET['userID']);
 $code = $_GET['mdcode'];
 $stmt = $user->query("SELECT * FROM users WHERE userID=:uid AND mdcode=:code");
 $stmt->execute(array(":uid"=>$id,":code"=>$code));
 $rows = $stmt->fetch(PDO::FETCH_ASSOC);
 if($stmt->rowCount() == 1)
 {
  if(isset($_POST['resetpass']))
  {
   $pass = $_POST['pass'];
   $cpass = $_POST['confirm-pass'];
   
   if($cpass!==$pass)
   {
    $msg = " <strong>Sorry!</strong>  Password Mismatch. ";
   }
   else
   {
    $stmt = $user->query("UPDATE users SET userPass=:upass WHERE userID=:uid");
    $stmt->execute(array(":upass"=>$cpass,":uid"=>$rows['userID']));
    
    $msg = "Password Changed.";
    header("refresh:5;index.php");
   }
  } 
 }
 else
 {
  exit;
 }
 }

?>
<!DOCTYPE html>
<html>
  <head>
    <title>Password Reset</title>
  </head>
  <body >
  <div>
  <strong>Hello !</strong>  <?php echo $rows['userEmail'] ?> you are here to reset your forgetton password.
  </div>
        <form method="post">
        <h3>Password Reset.</h3><hr />
        <?php
        if(isset($msg))
  {
   echo $msg;
  }
  ?>
        <input type="password" placeholder="New Password" name="pass" required />
        <input type="password" placeholder="Confirm New Password" name="confirm-pass" required />
        <button type="submit" name="resetpass">Reset Your Password</button>
        
      </form>
  </body>
</html>

In this step we get the new password and update the password.
That's all, this is how to Create Password Reset System Using PHP. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Comments

Popular posts from this blog

Contact Management System project using C with source code

Contact Management System (CMS)   A Contact Management System (CMS) is an essential application for organizing and managing contacts efficiently. Here's a simple implementation of a Contact Management System using C, allows users to perform operations such as adding, viewing, searching, modifying, and deleting contacts through a simple and user-friendly console interface. The CMS provides a practical demonstration of fundamental programming concepts such as arrays, structures, and string manipulation, making it an excellent project for students and beginners in C programming.   Project Overview The Contact Management System is designed to store basic information about contacts, including: Name: name of the person. Phone Number: phone number of the contact. Email Address: email address of the contact.   With this information, the program offers the following functionalities: Add New Contact...

Implementing Basic Add to Cart Functionality in Python Using Flask

In e-commerce websites, the "Add to Cart" feature allows users to select products they want to purchase and store them temporarily while they continue shopping. Implementing this functionality in a web application using Python and Flask is straightforward and can be done with a few simple steps.       Setting Up the Project: First, create a new directory for your project and set up a virtual environment to manage dependencies. Install Flask using pip and create three main files: `app.py`, `index.html`, and `cart.html`.   Read also:   Unlocking the Power of Free Google Tools for Seo Success Ultimate Guide: Google Search Console Crawl Reports you need to know Monitor What is dofollow backlinks: The ultimate 5 Benefit for your websites SEO Schema markup and How can you use schema markup for seo      Creating HTML Templates: Create HTML templates for displaying products (`index.html`) and the shopping cart (`cart.html`). These templates will use ...

Advanced HTML Concepts: Techniques and Examples for Modern Web Development

Advanced HTML goes beyond the basics of creating simple web pages, delving into powerful features and techniques that improve web interactivity, accessibility, and performance. It includes concepts such as semantic HTML, custom data attributes, responsive images with <picture> , enhanced forms, interactive content using <details> and <summary> , and embedding external resources with <iframe> .          Furthermore, advanced HTML focuses on SEO optimization through meta tags, accessibility improvements using ARIA, creating reusable Web Components, and optimizing performance with async and defer scripts.    Semantic HTML   Semantic elements clearly describe their meaning in a human- and machine-readable way.   <header> <h1>Welcome to My Website</h1> </header> <nav> <ul> <li><a href="home">Home...

Python Flask Student List CRUD System Using MySQL Database for Beginners

  In this tutorial, we are going to learn the python flask student list crud system using MySQL database for beginners. Before you have to learn how to python flask setting up and connect MySQL database connection and templates uses.    Read also: Python Flask tutorial Setting up a Flask and Sql Database on the create project folder you have to install two things to fetch data and flask. Here, below code to install using python terminal, and for MySQL, am using xampp software and database inside environment to turn on the apache server and MySQL database.   Read also: Python Flask with Mysql Database Import Flask and MySQL: from flask import Flask, render_template, request, redirect, url_for from flask_mysqldb import MySQL MySQL Database Connection: app = Flask(__name__) app.config['MYSQL_HOST'] = 'localhost' app.config['MYSQL_USER'] = 'root' app.config['MYSQL_PASSWORD'] = '' app.config['MYSQL_DB'] ...

Microdata Schema : How can Microdata schema Improve SEO with Examples

In this article, we will explore what microdata schema is, how it works, and provide some examples to help you better understand its importance. What is Microdata Schema? Microdata schema is a markup language that provides a standardized way of adding structured data to web pages. It uses HTML tags to describe information about the content on a web page, such as product reviews, events, recipes, and more. This makes it easier for search engines to crawl and index the content on a web page, which can help improve the visibility and ranking of the website. How Does Microdata Schema Work? Microdata schema uses a set of properties and item types to describe the content on a web page. Properties describe specific attributes of an item, while item types describe the type of item being described. For example, the property "name" might be used to describe the name of a person, while the item type "Person" would be used to indicate that the content being describ...

How to Create a Personal details information program in HTML

In this tutorial, we are learning how you can create a personal details information program in HTML with the simple using of HTML tags. HTML Form: <div class="container"> <h1 style="text-align:center">Personal Details</h1> <form class="form-control"> <label for="name">Name:</label><br> <input type="text" id="name" name="name"><br> <label for="email">Email:</label><br> <input type="email" id="email" name="email"><br> <label for="dob">Date of Birth:</label><br> <input type="date" id="dob" name="dob"><br> <label for="gender">Gender:</label><br> <input type="radio" id="gender" name="gender" value="male...