Forget Password Feature
In Day 15, we created signup page for our application. Since we take a long break, many of you have forget the password used to signup.
So lets make a forget password feature.
You may delete everything from server and keep Day 15 programs, so it will be less cluttered. Now onwards we will be doing an application to manage TODO list.
Home Page For our TODO Application
Lets create a very basic HTML page as our home page. We can make it look better, for now, this is a skeleton.
index.html
HTML Code:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>ToDo Application</h1>
<a href="signup.html">Signup</a><br>
<a href="lost_password.html">Lost Password</a><br>
</body>
</html>
This page links to 2 HTML pages. signup.html and lost_password.html. Both will need PHP files with same name.
signup.html and signup.php are covered in Day 15.
Today we will make lost_password.html and lost_password.php
Lost Password
If user forget his password, can get password using lost password feature. This is done in 2 parts to make it simple.
lost_password.html
HTML Code:
<!DOCTYPE html>
<html>
<head>
<title>Lost Password</title>
</head>
<body>
<h1>Lost Password</h1>
<form method="post" action="lost_password.php">
Enter Email<br>
<input type="text" name="email"><br>
<button type="submit" name="submit">Get My Password</button>
</form>
</body>
</html>
This is simple HTML form page. User can input his email address used to create account. When user click the submit button. we pass the FORM to lost_password.php using post method.
lost_password.php
This PHP will read email address user submitted from $_POST variable. Then connect to database, find user having the email address.
PHP Code:
<?php
if (isset($_POST['email'])) {
$email = $_POST['email'];
} else {
die('Please enter your email address');
}
// now we have $email variable.
// $email can contain space if user enter white space..
// So lets remove any white space char using trim() function.
$email = trim($email);
// lets connect to database.
// These lines are cut and paste from
// our previous signup.php
// No need to remember, just cut and paste.
$db_server = '127.0.0.1';
$db_user = 'fwhphp_user';
$db_password = 'k5BJRaX6SFbs';
$db_name = 'fwhphp_db';
$mysqli = new mysqli($db_server, $db_user, $db_password, $db_name);
if ($mysqli->connect_errno) {
echo 'Connect failed: ' . $mysqli->connect_error;
exit();
}
// lets write an SQL statement to search our users table
// to find the user having email address == $email
$sql = "select * from users where email='$email'";
// In above line, we used single quotes between email, because it is a string.
// Now time to execute the SQL statement.
$result = $mysqli->query($sql);
// lets see how many records found in database
// If num_rows is 0, no user found
if ($result->num_rows == 0) {
die("User with email address <b>$email</b> not found");
}
// lets get the user record from MySQLi result
$userInfo = mysqli_fetch_assoc($result);
// $userInfo is an associate array.
// Lets print it.
// Now you can see all information about the user, including password.
echo '<pre>';
print_r($userInfo);
// For real life application, we will sent password to users email
// address instead of printing it to browser.
// But for now, this is fine for us. We fix these issues when our application is ready to go live.
Try to understand the PHP script. Since we will be going do to PHP project in coming days, we will be doing codes like this. I tried to comment most of the lines, so you can understand what each line do.
If you have any doubt, ask here or ask others and make sure you understand and we will be doing these type of code rest of the days.
Exercise
Instead of showing password on browser, make its end a mail to user. Some hints
PHP Code:
$name = $userInfo['name'];
$password = $userInfo['password'];
mail($email,"Your password", "Hello $name, your password is $password");
Hope you can do the rest.
Bookmarks