PHP Forgot Password Recovery by Email

      Comments Off on PHP Forgot Password Recovery by Email

PHP Forgot Password Recovery by Email

php forgot password email

PHP Forgot Password Recovery by Email is mandatory for website which provides functionality after user registration. User try to setup different password, But on next login he/she forget what’s password he/she has setup, Here is quick solution with sample code.

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 Forgot Password Recovery by Email contains two basic concepts, (1) Fetch password from the database and (2) Send password by email. 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: Database Settings:

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

Step 2: Table detail:

  • Table Name: user_master
  • Field 1: user id, int
  • 2: username, varchar (20)
  • 3: password, varchar (20)
  • 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’);