In this article i am going to explain how to reset password using PHP,Mysql.
There are many methods available for password reset.In this i have used php email function to reset the password.I have used both $_POST and $_GET method for this.
If not registered user entered email address it shows error,if email was exists in database shows success message and sent mail with reset link.
The email contains reset link the user.if you click the link it will go to password reset page.
Functionality files:
1.forgotpassword.php
2.resetpassword.php
In this you will learn the following things.
1.Send mail using PHP
2.UPDATE Query
Please share your comments and feedback.Thanks.
There are many methods available for password reset.In this i have used php email function to reset the password.I have used both $_POST and $_GET method for this.
If not registered user entered email address it shows error,if email was exists in database shows success message and sent mail with reset link.
The email contains reset link the user.if you click the link it will go to password reset page.
Functionality files:
1.forgotpassword.php
2.resetpassword.php
In this you will learn the following things.
1.Send mail using PHP
2.UPDATE Query
Form
<form name="forgotpwdform" action="#" method="post">
<h3><a href="#" title="Forgot Password">Forgot Password</a></h3>
<label>Email</label>
<input type="email" name="Email" required/>
<p>
<input type="submit" name="ForgotPassword" value="Submit" id="submit"/>
</p>
</form>
<h3><a href="#" title="Forgot Password">Forgot Password</a></h3>
<label>Email</label>
<input type="email" name="Email" required/>
<p>
<input type="submit" name="ForgotPassword" value="Submit" id="submit"/>
</p>
</form>
Mail function with reset option
<?php
if(isset($_POST['ForgotPassword'])){
$email=$_POST['Email'];
//Query
$Password=mysql_query("SELECT Password FROM users WHERE Email='$email'");
$NumRows=mysql_num_rows($Password);
if($NumRows == '0') {
echo 'Sorry..Your Email not found in our database. Please try again';
header('Location:forgotpassword.php');
exit();
} else {
$to=$email; //To Mail
$subject="Password Assistance"; //Subject
$headers = "From: yourweb@website.in\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "X-Priority: 1\n";
$headers .= "X-MSmail-Priority: High\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n" ;
//Message
$message = " Hi ,\r\n";
$message .= "To initiate the password reset process for your $Email Account, click the link below:\r\n";
$message .= "http://www.yourwebsite.com/resetpassword.php?Email=$email\r\n";
$message .= "Cheers,\r\n";
$message .= " Team\n\n";
//Mail Script
$sentmail=mail($email, $subject, $message,$headers);
}
if($sentmail) {
echo 'Your Password reset link Has Been Sent To Your Email Address.Please check your Inbox!';
header('Location:forgotpassword.php');
exit();
} else {
echo 'Error.Please try again!';
header('Location:forgotpassword.php');
exit();
}
}
?>
if(isset($_POST['ForgotPassword'])){
$email=$_POST['Email'];
//Query
$Password=mysql_query("SELECT Password FROM users WHERE Email='$email'");
$NumRows=mysql_num_rows($Password);
if($NumRows == '0') {
echo 'Sorry..Your Email not found in our database. Please try again';
header('Location:forgotpassword.php');
exit();
} else {
$to=$email; //To Mail
$subject="Password Assistance"; //Subject
$headers = "From: yourweb@website.in\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "X-Priority: 1\n";
$headers .= "X-MSmail-Priority: High\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n" ;
//Message
$message = " Hi ,\r\n";
$message .= "To initiate the password reset process for your $Email Account, click the link below:\r\n";
$message .= "http://www.yourwebsite.com/resetpassword.php?Email=$email\r\n";
$message .= "Cheers,\r\n";
$message .= " Team\n\n";
//Mail Script
$sentmail=mail($email, $subject, $message,$headers);
}
if($sentmail) {
echo 'Your Password reset link Has Been Sent To Your Email Address.Please check your Inbox!';
header('Location:forgotpassword.php');
exit();
} else {
echo 'Error.Please try again!';
header('Location:forgotpassword.php');
exit();
}
}
?>
Password Reset Form
<form accept-charset="UTF-8" name="ProfileUpdate" method="post" action="#" id="reset-form" style="width:590px">
<h3>Reset Password</h3>
<div >
<div>
<label>New Password</label>
</div>
<div>
<input type="password" name="Password1" id="Password1" class="user-text" />
</div>
</div>
<div>
<div>
<label>Confirm New Password</label>
</div>
<div >
<input type="password" name="Password2" id="Password2" class="user-text1"/>
</div>
</div>
<div>
<p>
<input type="submit" name="ResetPassword" value="Submit" id="submit" />
</p>
</div>
</form>
<h3>Reset Password</h3>
<div >
<div>
<label>New Password</label>
</div>
<div>
<input type="password" name="Password1" id="Password1" class="user-text" />
</div>
</div>
<div>
<div>
<label>Confirm New Password</label>
</div>
<div >
<input type="password" name="Password2" id="Password2" class="user-text1"/>
</div>
</div>
<div>
<p>
<input type="submit" name="ResetPassword" value="Submit" id="submit" />
</p>
</div>
</form>
Reset function in PHP
<?php
//Database config
$connection=mysql_connect("localhost","root","");
$db=mysql_select_db("campustiger",$connection);
// Check connection
if (!$connection) {
die('Could not connect: ' . mysql_error());
}
if(isset($_POST['ResetPassword'])) {
$currentpassword = $_POST['Password']; // get the current password from
$newpassword = $_POST['Password1']; // get the new password from the
$hash = md5($newpassword); // encrypt the current password supplied by the user
$email = $_GET['Email'];
$result = mysql_query("SELECT Password from users where Email='$email'"); // query the database for getting password for a the user.
while($row = mysql_fetch_array($result)) {
$p=$row['Password'];
}
// get the encrypted password from the database for the user
?>
<?php
$result = mysql_query("UPDATE users set Password='$hash' where Email='$email' ");
if($result) {
header('Location:login.php');
}
else {
echo 'OOPS!.Please try again';
header('Location:resetpassword.php');
exit();
}
}
?>
//Database config
$connection=mysql_connect("localhost","root","");
$db=mysql_select_db("campustiger",$connection);
// Check connection
if (!$connection) {
die('Could not connect: ' . mysql_error());
}
if(isset($_POST['ResetPassword'])) {
$currentpassword = $_POST['Password']; // get the current password from
$newpassword = $_POST['Password1']; // get the new password from the
$hash = md5($newpassword); // encrypt the current password supplied by the user
$email = $_GET['Email'];
$result = mysql_query("SELECT Password from users where Email='$email'"); // query the database for getting password for a the user.
while($row = mysql_fetch_array($result)) {
$p=$row['Password'];
}
// get the encrypted password from the database for the user
?>
<?php
$result = mysql_query("UPDATE users set Password='$hash' where Email='$email' ");
if($result) {
header('Location:login.php');
}
else {
echo 'OOPS!.Please try again';
header('Location:resetpassword.php');
exit();
}
}
?>