Bladephp: Now with 8+ codeigniter, php, and laravel project 8+ php project From $37/Month From Rs. 2499/Month

Forgot Password System using PHP

by
Category:php
php
Forgot Password System using PHP

Forgot Password system is very neccessary for websites which has user registration feature because in case when user forgot his password and unable to login. You may also like How to generate random password using PHP and MySQL and How to Create Password Protected Webpage Using PHP.

HTML Code

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

PHP Code (send_password.php)

<?php
if(isset($_POST['submit_email']))
{
    $select=mysqli_query($conn,"select email,password from users where email = '".$_POST['enail']."' ");
    if(mysqli_num_rows($select) > 0)
    {
        $row=mysqli_fetch_array($select);
        $email=md5($row['email']);
        $pass=md5($row['password']);
        $msg ="<a href='www.bladephp.co/reset_password.php?key=".$email."&reset=".$pass."'>Click To Reset password</a>";
        $to = 'email@gmail.com';
        $subject = 'Forgot Password';
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
        $headers .= "From:demo<example@gmail.com>" . "\r\n";
        mail($to, $subject, $msg, $headers);
        echo "Check Your Email and Click on the link sent to your email";
    }	
}
?>

PHP and HTML Code (reset_password.php)

<?php
if(!empty($_GET['key']) && !empty($_GET['reset']))
{
  $email=$_GET['key'];
  $pass=$_GET['reset'];
  $select=mysqli_query($conn,"select email,password from users where md5(email)='$email' and md5(password)='$pass'");
  
  if(mysqli_num_rows($select) > 0)
  {
    ?>
    <form method="post" action="reset_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="save_password">
    </form>
    <?php
  }
}
?>

PHP Code (reset_new.php)

<?php
if(isset($_POST['save_password']))
{
  $email=$_POST['email'];
  $pass=$_POST['password'];
  $select = mysqli_query($conn,"update users set password='$pass' where email='$email'");
  echo "success";
}
?>

0 Comments


Html code work in comment box


Related Post