Home > PHP > How to Send Reset Password Link Email Using PHP?

How to Send Reset Password Link Email Using PHP?

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.

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.

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.

Photo of author

About Aman Mehra

Hey there! I'm Aman Mehra, a full-stack developer with over six years of hands-on experience in the industry. I've dedicated myself to mastering the ins and outs of PHP, WordPress, ReactJS, NodeJS, and AWS, so you can trust me to handle your web development needs with expertise and finesse. In 2021, I decided to share my knowledge and insights with the world by starting this blog. It's been an incredible journey so far, and I've had the opportunity to learn and grow alongside my readers. Whether you're a seasoned developer or just dipping your toes into the world of web development, I'm here to provide valuable content and solutions to help you succeed. So, stick around, explore the blog, and feel free to reach out if you have any questions or suggestions. Together, let's navigate the exciting world of web development!

Leave a Comment