User Account activation by email verification using PHP
User Account activation, email verification using PHP, MySQL and Swiftmailer
Introduction:
I have posted tutorials on login registration system, which has simple account registration process to just enter the details to create new account and you can login to the account, but we always needs to verify account email under live application to make sure user is using live email account for further use.I am writing this tutorial for those users who want to implement email activation feature in to the PHP application.
Please make sure we will be using Swiftmailer to send emails, if you are not familiar with swiftmailer you can read the following tutorial to read about sending emails using swiftmailer : PHP Email Sending with SwiftMailer
This tutorial is having following features:
- Register New User
- Send account activation email with verification link
- Verify user with verification link
- User login
- PHP
- MySQL
- Bootstrap 3.3.7
- Swift Mailer
Step 1: Application Directory and Database setup
First step is to setup application directly under web application directory, it can be anything it is totally depend on your Operating System and work environment setup.I am going to assume that you have created a empty directory to use for our login registration application.
Second step is to create new MySQL database with users table to use a backend for this application, if you already having a database setup then you can skip this step.
Use following command to create you database:
Use following SQL query to create new table called
users
:id
: Primary key with auto incrementstatus
: not null default is set to0
, zero means a user is inactiveemail_activation_key
: this field is going to have temporary activation code will be using it while doing verification process.
Step 2: Design Registration and Login Forms:
Before going to start designing we will need to download bootstrap framework fromgetbootstrap.com
,
please visit the site and download required version of bootstrap
framework. It is good practice to use design frameworks while developing
the web application it helps to boost our development process bootstrap
is one of the best example.If you’re done with the download extract the file and copy and paste
bootstrap-3.3.7-dist
folder to your application directory.Create new
index.php
page and use following script:Let’s have a quick look on our forms:
Register
:Login
:Step 3: Create Database connection script:
This is very basic step but required, we needs to create new file and add database connection script to be able to connect to the database whenever required.Create new file called
script/database_connection.php
and add following script, please make sure to update required variable to match with your system configuration:Step 4: Install swiftmailer:
As I said we would be using swiftmailer for outgoing emails, so in this step we will install swiftmailer to the applicationThere are few different ways to install
swiftmailer
to
the application here I will be using composer to get that pulled in, if
you don’t have composer installed I would suggest to install composer or
clone swiftmailer from Github repository. If you still don’t want to do
both of the above steps you can simple download it from the Github,
anyways let’s install it by using composerOpen Terminal/command line and use following command to install swiftmailer library:
Step 5: Create Library with necessary Functions:
We have our basic forms, database connection script and swiftmailer ready to use, now we will be using a central library file with class and functions to use while creating user, login user and send activation email.Go ahead and create file in the application called
lib/library.php
,
make sure to add this under lib folder just to keep this practice in
hand to have better management of our source, all right so we have
library.php file ready to use:Add a following script to it, which is have functions to Register new User, Login active user, Send activation email and so on.
Detail Description:
If you have a look on above file we have several functions in the library to perform operations:Register()
: used to create a new users, it requires four parameters first_name
, last_name
, email
and password
after creating new user it is going to call another function to send activation email.sendEmail()
: used to send activation
email to the user using swiftmailer, please make sure to change required
user email and password under SMTP settings.isEmail()
: to check whether email is
exists in the application, this is going to be usefull while creating
new user as well as login existing user.getUserID()
: use to get user ID from the database by using activation key which we are going to send with the email while creating new user.getUserIDByEmail()
: as name suggest, it used to get User ID by using user email address.activateAccount()
: used to activate the user by using User ID, we are going to call this function from activation.php
page.isValidPassword()
: validated the given password while login, we are using password_verify()
function to validated the password which is encrypted by password_hash()
function.findPasswordByEmail()
: finding the encrypted password by email.isActive()
: check whether given email address is active or inactive.UserDetails()
: used to get user details to display on profile page.Step 6: Add required HTML email files:
If you checksendEmail()
function you will see that we
need html and plain text content while sending email from swiftmailer,
this files are going to have the email content along with the activation
link.Go ahead and create following files and add give source to it:
email/email_verification.html
email/email_verification.txt
Step 7: Register User:
Open up theindex.php
file, which is having login and register form, let’s add register feature there.Add following line of code at the top of the index.php page.
You should get the email similar following email into your mailbox, verification links key should be different as it is randomly generated from the script.
Step 8: Create Activation Page:
We have added theactivation.php
page into the email link now we need to create the page to handle the activation request.activation page is going to access the input request from URL with the variable name called
key
and it is going to check the user associted and update the status field is equal to 1 to say it is activate.We are almost done with the registration and activation part, let’s focus on to the login request.
Step 9: Handle Login Request:
Open up theindex.php
page into your editor and add following code to validate and handle the login request.Step 10: Create Profile Page:
Createprofile.page
page to show user details and logout link:Step 11: Logout page:
Finally use following script to create logout.php page to clear session and logout the user.Tutorial Folder Structure:
We are done!
If you get any question or issues related to this tutorial you can always let me know using comment box below or you can also send me an direct email using contact us page, thank you!