Do you want to be a good Web Developer?

Enter Your Email To Get Our Exclusive Tutorials
And Offer Right In Your Email

Join TalkersCode Community And Recieve Exclusive Tutorials
On Web Development Right Via Email.

Password Reset System Using PHP

Monday, Jan 9, 2017

Tags:- PHP HTML SMTP


Password reset system is very neccessary for websites which has user registration feature because in case when user forgot his password and unable to login and then this password reset system helps to reset the password by sending link to his registered email address.In this tutorial we will create a password reset system using PHP.You may also like Account Verification System Through Email Using PHP


Password Reset System Using PHP


To Create Password Reset System it takes only four steps:-

  1. Make a HTML file and define markup for password reset system
  2. Make a PHP file to send the link
  3. Make a PHP file to reset Password
  4. Make a PHP file to update new password


Step 1.Make a HTML file and define markup for password reset system

We make a HTML file and save it with a name reset_pass.html

<html>
  <body>
    <form method="post" action="send_link.php">
      <p>Enter Email Address To Send Password Link</p>
      <input type="text" name="email">
      <input type="submit" name="submit_email">
    </form>
  </body>
</html>

In this step we make a password reset form in this user enter his registered email id.Before proceed further download PHP Mailer Library to send link to user registered email address



Step 2.Make a PHP file to send the link

We make a PHP file and save it with a name send_link.php

<?php
if(isset($_POST['submit_email']) && $_POST['email'])
{
  mysql_connect('localhost','root','');
  mysql_select_db('sample');
  $select=mysql_query("select email,password from user where email='$email'");
  if(mysql_num_rows($select)==1)
  {
    while($row=mysql_fetch_array($select))
    {
      $email=md5($row['email']);
      $pass=md5($row['password']);
    }
    $link="<a href='www.samplewebsite.com/reset.php?key=".$email."&reset=".$pass."'>Click To Reset password</a>";
    require_once('phpmail/PHPMailerAutoload.php');
    $mail = new PHPMailer();
    $mail->CharSet =  "utf-8";
    $mail->IsSMTP();
    // enable SMTP authentication
    $mail->SMTPAuth = true;                  
    // GMAIL username
    $mail->Username = "your_email_id@gmail.com";
    // GMAIL password
    $mail->Password = "your_gmail_password";
    $mail->SMTPSecure = "ssl";  
    // sets GMAIL as the SMTP server
    $mail->Host = "smtp.gmail.com";
    // set the SMTP port for the GMAIL server
    $mail->Port = "465";
    $mail->From='your_gmail_id@gmail.com';
    $mail->FromName='your_name';
    $mail->AddAddress('reciever_email_id', 'reciever_name');
    $mail->Subject  =  'Reset Password';
    $mail->IsHTML(true);
    $mail->Body    = 'Click On This Link to Reset Password '.$pass.'';
    if($mail->Send())
    {
      echo "Check Your Email and Click on the link sent to your email";
    }
    else
    {
      echo "Mail Error - >".$mail->ErrorInfo;
    }
  }	
}
?>

In this step we get the user entered email and check if user exist in database or not but before check always validate user entered data or you can check our How To Validate Data Before And After Submit. And if the user exist in our database then we send hashed email and password on link to his registered email id you can check our Send Mail Using PHP to send email using Gmail.



Step 3.Make a PHP file to reset Password

We make a PHP file and save it with a name reset_pass.php

<?php
if($_GET['key'] && $_GET['reset'])
{
  $email=$_GET['key'];
  $pass=$_GET['reset'];
  mysql_connect('localhost','root','');
  mysql_select_db('sample');
  $select=mysql_query("select email,password from user where md5(email)='$email' and md5(password)='$pass'");
  if(mysql_num_rows($select)==1)
  {
    ?>
    <form method="post" action="submit_new.php">
    <input type="hidden" name="email" value="<?php echo $email;?>">
    <p>Enter New password</p>
    <input type="password" name='password'>
    <input type="submit" name="submit_password">
    </form>
    <?php
  }
}
?>

In this step get both the hashed email and pass and check both in database if both exist and then we display new password form to the user



Step 4.Make a PHP file to update new password

We make a PHP file and save it with a name submit_new.php

<?php
if(isset($_POST['submit_password']) && $_POST['key'] && $_POST['reset'])
{
  $email=$_POST['email'];
  $pass=$_POST['password'];
  mysql_connect('localhost','root','');
  mysql_select_db('sample');
  $select=mysql_query("update user set password='$pass' where email='$email'");
}
?>

In this step we get the new password and update the password.


Thats all, this is how to Create Password Reset System Using PHP.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Join Us With Our 21000+ Subscribers And Get Our Latest Tutorials Update Via Email