X
    Categories: PHP

Forget password script in php

Forget password script in php

Hi In this tutorial we are going to see how to reset the forget password using php code.
The Logic I follows to reset forget password is :

1. Enter username or email.
2. If username or email exist it will send a mail to certain user with password reset form link.
3. Else return with no records found.
4. By clicking reset password link user can update new password.

My Database Name : 17educations_demo

My Table name : members

Step 1 :

Create Database with name of 17educations_demo

Step 2 :

Create members table and insert one static record for demo use.

--
-- Table structure for table `members`
--

CREATE TABLE IF NOT EXISTS `members` (
`member_id` int(8) NOT NULL,
  `member_name` varchar(255) CHARACTER SET utf8 NOT NULL,
  `member_password` varchar(64) NOT NULL,
  `member_email` varchar(255) CHARACTER SET utf8 NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `members`
--

INSERT INTO `members` (`member_id`, `member_name`, `member_password`, `member_email`) VALUES
(1, 'marimuthu', 'e2f3088a505f1ed02e40f5b62550f291', 'marimuthu@gmail.com');

Step 3 :

Create Password reset request form

index.php

<?php
	if(!empty($_POST["forgot-password"])){
		$conn = mysqli_connect("localhost", "root", "", "17educations_demo");
		
		$condition = "";
		if(!empty($_POST["username"])) 
			$condition = " member_name = '" . $_POST["username"] . "'";
		if(!empty($_POST["email"])) {
			if(!empty($condition)) {
				$condition = " and ";
			}
			$condition = " member_email = '" . $_POST["email"] . "'";
		}
		
		if(!empty($condition)) {
			$condition = " where " . $condition;
		}

		$sql = "Select * from members " . $condition;
		$result = mysqli_query($conn,$sql);
		$user = mysqli_fetch_array($result);
		
		if(!empty($user)) {
			require_once("forgot-password-recovery-mail.php");
		} else {
			$error_message = 'No User Found';
		}
	}
?>

<link href="demo-style.css" rel="stylesheet" type="text/css">
<script>
function validate_forgot() {
	if((document.getElementById("username").value == "") && (document.getElementById("email").value == "")) {
		document.getElementById("validation-message").innerHTML = "Login name or Email is required!"
		return false;
	}
	return true
}
</script>
<form name="frmForgot" id="frmForgot" method="post" onSubmit="return validate_forgot();">
<h1>Forgot Your Password?</h1>
	<?php if(!empty($success_message)) { ?>
	<div class="success_message"><?php echo $success_message; ?></div>
	<?php } ?>

	<div id="validation-message">
		<?php if(!empty($error_message)) { ?>
	<?php echo $error_message; ?>
	<?php } ?>
	</div>

	<div class="field-group">
		<div><label >
Here I will check wheather user details exists. If exists I have included forgot-password-recovery-mail.php page which contains the code to send mail to the user.

Step 4 :

forgot-password-recovery-mail.php
<?php
if(!class_exists('PHPMailer')) {
    require('phpmailer/class.phpmailer.php');
	require('phpmailer/class.smtp.php');
}

require_once("mail_configuration.php");

$mail = new PHPMailer();

$emailBody = "<div>Hi " . $user["username"] . ",<br><br><p>Please, Click this link to recover your password<br><a href='" . PROJECT_HOME . "forgot-password/reset_password.php?name=" . $user["username"] . "'>" . PROJECT_HOME . "forgot-password/reset_password.php?name=" . $user["username"] . "</a><br><br></p>Regards,<br> Admin.</div>";

$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "tls";
$mail->Port     = PORT;  
$mail->Username = MAIL_USERNAME;
$mail->Password = MAIL_PASSWORD;
$mail->Host     = MAIL_HOST;
$mail->Mailer   = MAILER;

$mail->SetFrom(SERDER_EMAIL, SENDER_NAME);
$mail->AddReplyTo(SERDER_EMAIL, SENDER_NAME);
$mail->ReturnPath=SERDER_EMAIL;	
$mail->AddAddress($user["email"]);
$mail->Subject = "Forgot Password Recovery";		
$mail->MsgHTML($emailBody);
$mail->IsHTML(true);

if(!$mail->Send()) {
	$error_message = 'Problem in Sending Password Recovery Email';
} else {
	$success_message = 'Please check your email to reset password!';
}

?>

IF you don't habe class.phpmailer.php and class.smtp.php please download latest file from here
https://github.com/PHPMailer/PHPMailer/blob/master/class.phpmailer.php


Step 5 :

mail_configuration.php 

<?php
define("PROJECT_HOME","http://localhost/17educations/samples/");

define("PORT", ""); // port number
define("MAIL_USERNAME", ""); // smtp usernmae
define("MAIL_PASSWORD", ""); // smtp password
define("MAIL_HOST", ""); // smtp host
define("MAILER", "smtp");

define("SENDER_NAME", "Admin");
define("SERDER_EMAIL", "admin@admin.com");

Step 6 :

Password Reset form

<?php
if(isset($_POST["reset-password"])) {
$conn = mysqli_connect("localhost", "root", "", "blog_samples");
$sql = "UPDATE `blog_samples`.`members` SET `member_password` = '" . md5($_POST["password"]). "' WHERE `members`.`member_name` = '" . $_GET["name"] . "'";
$result = mysqli_query($conn,$sql);
$success_message = "Password is reset successfully.";

}
?>
<link href="demo-style.css" rel="stylesheet" type="text/css">
<script>
function validate_password_reset() {
if((document.getElementById("password").value == "") && (document.getElementById("confirm_password").value == "")) {
document.getElementById("validation-message").innerHTML = "Please enter new password!"
return false;
}
if(document.getElementById("password").value != document.getElementById("confirm_password").value) {
document.getElementById("validation-message").innerHTML = "Both password should be same!"
return false;
}

return true;
}
</script>
<form name="frmReset" id="frmReset" method="post" onSubmit="return validate_password_reset();">
<h1>Reset Your Password</h1>
<?php if(!empty($success_message)) { ?>
<div class="success_message"><?php echo $success_message; ?></div>
<?php } ?>

<div id="validation-message">
<?php if(!empty($error_message)) { ?>
<?php echo $error_message; ?>
<?php } ?>
</div>

<div class="field-group">
<div><label >
Here you can enter new password it will store in the members table based on username or email.

If you like this article don't forget to share and comment.
Thanks!.
Happy Coding!..

 

 

Marimuthu: