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)

 

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

 

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’);

?>

 

 

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