Reset password functionality is very useful to recover your account password. Suppose you had registered on a website and after some days you forget the password then here you will see the option on the website to recover your password.
So in this tutorial, you will learn the functionality of how to reset a password and send a link in an email using PHP to recover the password.
We will make a reset password form to get the email ID from users and then send a reset password link in their email with the limit of time expiration.
Let’s get started to make this reset password functionality in PHP. Follow the below steps one by one.
Create a Database Connection File in PHP
Create a “dbconnection.php” file in PHP and make a database connection with mysqli_connect()
function. See the following code for reference.
Create a Reset Password Form File in PHP
Create a file name “forget-password.php“, but you take the file name as you want. In this file, we will create a simple HTML form with an email field and submit button.
And make an action on another file to send the reset password email to users when they click on submit button.
Generate Token with Expiry Time and Send a Link to Email
Create a new file “send-token-email.php“. In this file, we will check whether the form is submitted or not then we verify the user is exist or not. If a user exists then generate a unique token to send in an email with expiration time.
It will update the users table with token and expiration time. Here I set the 1 hour expiration time but you set it as you need.
See Also: How to Add and Subtract Days, Month and Year From Date in PHP?
Finally, send an email to the user’s email with the reset password link. Users can click on that link and set the new password.
Create a Reset Password Form for New Password
Now we will create a new file called “reset-pssword.php” that has an HTML form to reset the new password. In this file, we will check the URL has required to GET parameters or not.
If parameters exist then we match them with database stored values to proceed further. If everything is ok then the users can set the new password using this form. The user has to type the new password and confirm the password.
Create a PHP File to Update New Password
Create an “update-password.php” file in your project. In this file, we will get the new password from above form and then update it users table.
See Also: How to Secure Password Using bcrypt in PHP?
Conclusion
So in this tutorial, you learned how to send a reset password link to email using PHP. You also learned how to set the expiration token time for resetting passwords.
So it is very simple in PHP. If you have followed the above steps carefully, I’m sure it will work for you. If you still have any doubts or get any errors please do let me know in the comment section.
See Also: How can I Prevent SQL Injection in PHP?