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)

CREATE TABLE IF NOT EXISTS `user_master` (
`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’, ‘contact@adarshspatel.in’);

Step 4: Download Supporting file and upload to Server

[sociallocker]Download [/sociallocker]

Step 5: prepare forget.php page as follow

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

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

<form method=”post” action=”forget2.php”>
Enter Email address : <input type=”text” name=”email”>
<br><br>
<input type=”submit” value=”Send me Password”>
</form>
<br>
<?php
if(isset($_SESSION[‘msg’]))
{
echo $_SESSION[‘msg’];
unset($_SESSION[‘msg’]);
}
?>
</body>
</html>

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

<?php
session_start();

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

//Connection…
$link = mysql_connect($hostname, $username, $password);

//Set Database
mysql_select_db($database,$link);

//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);
$toemailaddress=$row[’email’];
$password=$row[‘password’];

ini_set(‘display_errors’, 1);
error_reporting(~0);

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

ob_start();
require_once(‘./class.phpmailer.php’);
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet=”UTF-8”;
$mail->SMTPSecure = ‘ssl’;
$mail->Host = ‘smtp.gmail.com’;
$mail->Port = 465;
$mail->Username = ‘you@gmail.com’;
$mail->Password = ‘gmailpassword’;
$mail->SMTPAuth = true;

$mail->From = ‘From Email Address’;
$mail->FromName = ‘From Name’;
$mail->AddAddress(“$toemailaddress”);

$mail->IsHTML(true);
$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’);

?>