Forgot Password (Password Reset) system in php

Hey Technoz, Hope you are doing great! Today, we’ll see how to implement an important feature- forgot password or password reset system in our php website. In many circumstances, users may forgot their password, so this is a must have system for every dynamic website. So lets start.

Creating Forgot Password (Password Reset) system

Creating Database

As our website is dynamic and deals with user data, we need a database to store user entries in it. Simply create table ‘users’ and make 4 fields in it as id, name, email and password. You can use SQL query below to create the same.

Connection Files

We need to connect the system with the database. So create php file config.php and add code below.

Remember to replaces values with yours.

Download phpmailer library

As we are sending email with phpmailer library, we need to download and put it on our project. Go to this link and download phpmailer. Put it in root folder of your project.

Forgot Password Mechanism

Now, we need to write code for Forgot Password system, so create file forgot-pass.php and paste the below code in it.

The above code check if the entry exists in database. If so, then an email with the forgot password link will be sent to the user’s registered email address. The $message is assigned the whole email template which is sent as an email to the recipient. Lets look in the screenshot below after mail sent successfully.

Forgot Password (Password Reset) system in php

Password Reset Mechanism

Now, we have to handle the button click event sent to the user in the email. Create php file reset-pass.php and paste below code in it.

The url on the button is the link to above file, with GET parameter as id encoded in base64_encode() function, in the previous file.  This file takes encoded value of id, decodes it, and updates the record with the new password as typed by user with primary key as id. In this tutorial, we’ve not encrypted password to see the updated value. You can see updated value in the password column in database.

Lets have a look after successful password reset in image below.

Forgot Password (Password Reset) system in php

That’s it! It is working fine. Hope you liked it. Please subscribe our blog to receive more such knowledgeable and useful tutorials. If you have any problems, feel free to comment below. We’ll try our best to solve your query. Thanks:)

Download Source Code

If you want to directly jump on the implementation, you can always download source code for free of cost from below link.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.