If you are following from the previous scripts, then add one field in database as email, and add an email address in that field.

We will create two pages

  1. connect.php
  2. forgotpassword.php

Create connect.php file

If you are following from previous tutorials, you should have this file. Below code is to connecting database and selecting the database.

 $connection = mysqli_connect('localhost', 'root', 'Rvm@i[9)0?~=');
if (!$connection){
die("Database Connection Failed" . mysqli_error($connection));
}
$select_db = mysqli_select_db($connection, 'pixelw3p_demo');
if (!$select_db){
die("Database Selection Failed" . mysqli_error($connection));
}

Create a Form for Forgot Password

Here we will display only username field, based on the username will fetch the data to send email.

 

<form class="form-signin" method="POST">

<h2 class="form-signin-heading">Forgot Password</h2>


<div class="input-group"><span id="basic-addon1" class="input-group-addon">@</span>
<input class="form-control" name="username" required="" type="text" placeholder="Username" /></div>

<button class="btn btn-lg btn-primary btn-block" type="submit">Forgot Password</button>
<a class="btn btn-lg btn-primary btn-block" href="login.php">Login</a>

</form>

Add styles

Here I’m using Bootstrap to add styling and some custom styles.

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" >

<link rel="stylesheet" href="styles.css" >
<!-- Latest compiled and minified JavaScript -->
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%20src%3D%22https%3A%2F%2Fmaxcdn.bootstrapcdn.com%2Fbootstrap%2F3.3.7%2Fjs%2Fbootstrap.min.js%22%3E%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;script&gt;" title="&lt;script&gt;" />

Save this below code as styles.css file.

body {
padding-top: 40px;
padding-bottom: 40px;
background-color: #eee;
}</code>

.form-signin {
max-width: 330px;
padding: 15px;
margin: 0 auto;
}
.form-signin .form-signin-heading,
.form-signin .checkbox {
margin-bottom: 10px;
}
.form-signin .checkbox {
font-weight: normal;
}
.form-signin .form-control {
position: relative;
height: auto;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
padding: 10px;
font-size: 16px;
}
.form-signin .form-control:focus {
z-index: 2;
}
.form-signin input[type="email"] {
margin-bottom: -1px;
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
}
.form-signin input[type="password"] {
margin-bottom: 10px;
border-top-left-radius: 0;
border-top-right-radius: 0;
}

PHP Code to Send Forgotten Password by E-Mail

I’m including connect.php file for connecting to database, add this below line of code before opening html tag.

require_once('connect.php');

After that check if post super global is set & not empty, then assign submitted username to variable. Check that username exists in database using select query, select query should return one record. We can test that using mysqli_num_rows php function. Below is the code.

if(isset($_POST) &amp; !empty($_POST)){
$username = mysqli_real_escape_string($connection, $_POST['username']);
$sql = "SELECT * FROM `login` WHERE username = '$username'";
$res = mysqli_query($connection, $sql);
$count = mysqli_num_rows($res);
if($count == 1){
echo "Send email to user with password";
}else{
echo "User name does not exist in database";
}
}

If the select query result returns one row, then we will send the email with password that is fetched from select query. Assign values of email, password to variables fetched from select query.

Then add php mail function with to email address, subject line, message and from address. If the mail sent successfully then I’m dispalying success message or failure message.

$r = mysqli_fetch_assoc($res);
$password = $r['password'];
$to = $r['email'];
$subject = "Your Recovered Password";

$message = "Please use this password to login " . $password;
$headers = "From : vivek@codingcyber.com";
if(mail($to, $subject, $message, $headers)){
echo "Your Password has been sent to your email id";
}else{
echo "Failed to Recover your password, try again";
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here