php forgot password email

php forgot password email

php forgot password email

developing a website and stuck in forgot password page, don’t worry here is complete code which will help you to prepare forgot password page. In this PHP tutorial, i have explained all steps how you can create forgot password recovery by email page.

This PHP tutorial contains two basic concepts, concept one to fetch password from the database and concept two how to send email using PHP, for PHP sending you need to upload supported file to server and try to send email from server only, if you wish to send email from xampp or wamp then you must configure SMTP in your config file.

Note: This article is prepared for beginners only, This version/coding pattern can not be used directly while developing a live website.

Step 1: I assume that you have configured your database setting as follow:

  • Server: localhost
  • Username: root
  • Password: root
  • database: project

Step 2: Create the Following Table with given field

  • Table Name: user_master
  • column 1: user id, int
  • column 2: username, varchar (20)
  • column 3: password, varchar (20)
  • column 4 : email, varchar(255)


userid int(11) NOT NULL,
username varchar(20) NOT NULL,
password varchar(20) NOT NULL,
email varchar(255) NOT NULL



Step 3 : Enter Sample Insert Values for testing

INSERT INTO project.user_master (userid, username, password, email) VALUES (‘1’, ‘adarsh’, ‘adarsh’, ‘’);



 Step 4: Download Supporting file and upload to Server


Step 5: prepare forget.php page as follow

<?php session_start() ?>
<title> Forgot Password Recovery by Email</title>

<h2> Forgot Password Recovery by Email</h2>

<form method=”post” action=”forget2.php”>
Enter Email address : <input type=”text” name=”email”>
<input type=”submit” value=”Send me Password”>
echo $_SESSION[‘msg’];


Step 6: prepare forget2.php page as follow: (add your Gmail email address and password in the following code)


//============= Variables for Database ===================
$hostname = “localhost”;
$username = “root”;
$password = “root”;
$database = “project”;

$link = mysql_connect($hostname, $username, $password);

//Set Database

//Read Form Data from Page1
$u = $_POST[’email’];

$query = “select * from user_master where email=’$u'”;
$result = mysql_query($query);

$row = mysql_fetch_array($result);

ini_set(‘display_errors’, 1);

$toemailaddress = “”;
$subjectline = “Check email for Your Password”;
$body =”Your Password is : “.$password;

$mail = new PHPMailer();
$mail->SMTPSecure = ‘ssl’;
$mail->Host = ‘’;
$mail->Port = 465;
$mail->Username = ‘’;
$mail->Password = ‘gmailpassword’;
$mail->SMTPAuth = true;

$mail->From = ‘From Email Address’;
$mail->FromName = ‘From Name’;

$mail->Subject    = “$subjectline”;
$mail->AltBody    = “To Read Email use HTML View”;
$mail->Body    = “$body”;

$t = $mail->Send();
//echo $t;
$_SESSION[‘msg’]=”Check email for password”;
header(‘Location: forget.php’);




Adarsh Patel

My Self Adarsh Suryakant Patel. Currently I am working as Assistant Professor at Parul Instiute of Engineering and Technology, MCA Department, Limda, Vadodara. Major Responsibility : * Lectures * Organizing Training and Expert Sessions for * Placement and Recruitment * Faculty Representative