AdyBlaze AdyBlaze
Latest Article

PHP Forgot Password System (2026) – Complete Beginner Guide

01 Aug 2026

WhatsApp Facebook Telegram


PHP Forgot Password System (2026)

A Forgot Password System allows users to securely reset their password when they forget it. Instead of revealing the old password, the system sends a secure password reset link to the user’s registered email address.

In this tutorial, you’ll learn how to build a PHP Forgot Password System using PHP 8.x and MySQL with password reset tokens and secure password hashing.

Read this also : PHP Logout System in PHP (2026) – Complete Beginner Guide with Example


What You’ll Learn


How the Forgot Password System Works

  1. User clicks Forgot Password.
  2. User enters their registered email.
  3. PHP checks whether the email exists.
  4. A unique reset token is generated.
  5. The token is saved in the database.
  6. A reset link is emailed to the user.
  7. The user opens the link and enters a new password.
  8. PHP updates the password and removes the token.

Project Structure

forgot-password/
│
├── db.php
├── forgot-password.php
├── send-reset.php
├── reset-password.php
├── update-password.php
└── login.php

Step 1: Create Database

CREATE DATABASE login_system;

Create users table:

CREATE TABLE users (

id INT AUTO_INCREMENT PRIMARY KEY,

name VARCHAR(100),

email VARCHAR(150) UNIQUE,

password VARCHAR(255),

reset_token VARCHAR(255),

token_expire DATETIME

);

Step 2: Database Connection

<?php

$conn=mysqli_connect(
"localhost",
"root",
"",
"login_system"
);

if(!$conn){

die("Connection Failed");

}

?>

Step 3: Forgot Password Form

<form action="send-reset.php" method="POST">

<input
type="email"
name="email"
placeholder="Enter Email"
required>

<button type="submit">
Send Reset Link
</button>

</form>

Step 4: Verify Email

$email=$_POST['email'];

$result=mysqli_query(
$conn,
"SELECT * FROM users
WHERE email='$email'"
);

if(mysqli_num_rows($result)==0){

die("Email Not Found");

}

Step 5: Generate Reset Token

$token=bin2hex(random_bytes(32));

$expire=date(
"Y-m-d H:i:s",
strtotime("+1 hour")
);

The reset link will remain valid for 1 hour.


Step 6: Save Token

mysqli_query(
$conn,

"UPDATE users

SET

reset_token='$token',

token_expire='$expire'

WHERE email='$email'"
);

Step 7: Send Reset Email

$link="https://example.com/reset-password.php?token=".$token;

mail(

$email,

"Password Reset",

"Click Here:\n".$link

);

Note: For production websites, use PHPMailer with SMTP instead of PHP’s basic mail() function.


Step 8: Verify Token

$token=$_GET['token'];

$result=mysqli_query(

$conn,

"SELECT *

FROM users

WHERE

reset_token='$token'

AND

token_expire>NOW()"

);

if(mysqli_num_rows($result)==0){

die("Invalid or Expired Link");

}

Step 9: Reset Password Form

<form action="update-password.php" method="POST">

<input
type="hidden"
name="token"
value="<?php echo $token; ?>">

<input
type="password"
name="password"
placeholder="New Password"
required>

<button type="submit">

Update Password

</button>

</form>

Step 10: Update Password

$password=password_hash(

$_POST['password'],

PASSWORD_DEFAULT

);

$token=$_POST['token'];

mysqli_query(

$conn,

"UPDATE users

SET

password='$password',

reset_token=NULL,

token_expire=NULL

WHERE reset_token='$token'"

);

echo "Password Updated Successfully.";

Why Use password_hash()?

Never store plain text passwords.

Use:

password_hash($password,PASSWORD_DEFAULT);

During login, verify using:

password_verify($password,$hash);

Common Errors

Reset Link Expired

Always set an expiration time for reset tokens.


Email Not Sending

Local servers like XAMPP usually don’t send emails by default. Configure SMTP or use PHPMailer.


Invalid Token

Ensure the token matches the database and has not expired.


Plain Password Storage

Never save passwords without hashing.


Best Practices


FAQs

Why use a reset token?

A reset token verifies that the password reset request is legitimate and prevents unauthorized users from changing passwords.


How long should a reset link remain valid?

A validity period of 30 to 60 minutes is commonly recommended.


Can I use mail() instead of PHPMailer?

Yes, but for production websites, PHPMailer with SMTP is more reliable and secure.


Should I store the token forever?

No. Delete the token immediately after the password is successfully updated.


Conclusion

A secure Forgot Password System is an essential part of any login application. By using reset tokens, password hashing, and email verification, you can protect user accounts while providing a simple way for users to recover access. Always follow modern security practices and avoid storing plain-text passwords.


SEO Keywords