Today i will explain how to reset your account password using PHPMailer, PHP and MySQL, in this tutorial i will implement Forgot Password Recovery (Reset) using PHP and MySQL. Before moving towards the reset your password first we need a user registration and login script in php, so if you do not know how to register user and login, you can check out my tutorial about Simple User Registration & Login Script in PHP and MySQL.
To implement forgot password recovery, i will suggest you all to download and set up user registration script so that you can add forgot password functionality in it. However, it is not mandatory if you are an advance user and you can integrate it in your project then you do not need to set it up.
In my user registration tutorial there is a table name users, we will use the same table to check is user exist or not. You will add files of this tutorial in user registration and login script folder.
We will send an email using PHPMailer, if you do not know how to user PHPMailer so you can check my PHPMailer tutorial, i have wrote a detailed tutorial about how to send email in PHP using PHPMailer.
Steps to Forgot Password Recovery (Reset) using PHP and MySQL
We have to follow these steps to implement forgot password functionality.
- Create a Temporary Token Table
- Create a Database Connection
- Create an Index File (Send Email)
- Create a Reset Password File
- Create a CSS File
Let me give you a quick review of it, first we will create a table to store a token valid for one day for any user. We will also create a form that will take input of email, then we will check either email exist or not, if email is found a temporary token will be generated and email will be sent to the user with the generated token.
Once user clicked on the email token link within one day, user can reset new password. For that purpose we will also create another form that will take input of new password and update it in user table and we will also remove the temporary token from temporary token table once user successfully updated password.
1. Create a Temporary Token Table
We need to create temporary token table, run the following query.
I have also attached sql file of this table in the download file of this tutorial.
2. Create a Database Connection
Create a database connection file with name db.php and add the following script in it, don’t forget to change your database credentials in this file.
We have also define the date timezone, you can set it as per your location. This helps to store data in the timezone of your location.
3. Create an Index File (Send Email)
Now create an index.php file that will take email input and send an email to the user if user is found in the users table. users table is available in the login and registration script, we are using the same table.
Add the following script in index.php file.
This file is simply checking if email is available in database then generate a random token, save that token in temporary table and send an email to the user with link. Once user click on the link user will be able to set new password.
Please note that i have been using https://www.allphptricks.com/forgot-password/ directory URL in the above script, it should be replace with your project URL where you will upload files of this tutorial.
4. Create a Reset Password File
Now create a rest password file, this will check that is token available in database against the user email and it should be less then one day old, once token expired user will need to regenerate token.
So if token is found user can simply set new password, we will update user password and also delete the token from temporary token table.
Insert the following script in reset-password.php file.
Please note that i have wrote https://www.allphptricks.com/forgot-password/ in these both files, make sure that you also update it as per your web directory URL. You will write your directory where you will set up user registration and login script.
5. Create a CSS File
Create a file with name style.css and keep it in folder css. Paste the following code in it.
If you found this tutorial helpful, share it with your friends and developers group.
I spent several hours to create this tutorial, if you want to say thanks so like my page on Facebook and share it.
No comments:
Post a Comment