Home Bootstrap 3 User Account activation by email verification using PHP

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:

  1. Register New User
  2. Send account activation email with verification link
  3. Verify user with verification link
  4. User login

Technology used:

  1. PHP
  2. MySQL
  3. Bootstrap 3.3.7
  4. Swift Mailer
Get Complete Source Code

Let’s get started:

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:

You can replace the database name with whatever you suggest just make sure to keep this in mind to replace while connecting from PHP script, now let’s create users tables with required fields.

Use following SQL query to create new table called users:

Users Table Design Overview
Users Table Design Overview

A quick overview on important fields of the table:

  • id : Primary key with auto increment
  • status: not null default is set to 0, zero means a user is inactive
  • email_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 from getbootstrap.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:

If you try to run this script on web browser you will see I have added two forms on single page as you know this a just demo to learn you can have two separate pages for each request, just make sure about the form action.

Let’s have a quick look on our forms:

Register:

Registration Form
Registration Form

Login:

Login Form
Login Form

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 application

There 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 composer

Open Terminal/command line and use following command to install swiftmailer library:

If you are getting output similar to below lines, meaning composer is downloading required package files.

We have swiftmailer ready to use, move on to the next step to create library file.

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 check sendEmail() 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

html email

email/email_verification.txt

You can change the content as per you requirement.

Step 7: Register User:

Open up the index.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.

if you try to run the current code you should be able to create new user:

user get created successfully

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.

Activation Email
Activation Email

Step 8: Create Activation Page:

We have added the activation.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.

If you try to click on the email link to test, this page needs work and it is going to show you a following response:

Activation Success Message

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 the index.php page into your editor and add following code to validate and handle the login request.

if you see the above code we are validating the user with the several conditions, please keep in mind that this is just a part of code from our existing index.php page, so needs to call the library file again as we have already added on top of the page.



Step 10: Create Profile Page:

Create profile.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:

Tutorial folder structure

We are done!

Next you can learn how you can provide change password feature for the active user from this tutorial Change User Password by Validating Current Password in PHP MySQL

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!

Get Complete Source Code

Enjoy this tutorial? Get iTech Empires updates delivered straight to your inbox.

No Spam, ever. We'll never share your email address and you can opt out at any time.

Yogesh Kolihttp://www.itechempires.com
Yogesh Koli is a software engineer & Blogger lives in India. He's driven by an addiction to learning and a love for adventure. he has 6+ years of experience working with the front-end and back-end web application development.

28 COMMENTS

avatar
11 Comment threads
16 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
11 Comment authors
RizkiYogesh KoliRuatememEmad WageehAmit pampaniya Recent comment authors

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

  Subscribe  
newest oldest most voted
Notify of
Rizki
Guest
Rizki

I’ve bought this code, but when register success (also success insert into database), alert doesnt appear ( $register_success_message), can you help me sir? please

Emad Wageeh
Guest
Emad Wageeh

I need composer.json and composer.lock files ,please

Ruatemem
Guest
Ruatemem

those files are auto-generated when you install swiftmailer inside the project directory

Amit pampaniya
Guest
Amit pampaniya

Notice: Undefined variable: app in C:wamp64wwwNew folder (5)vendorindex.php on line 20

Fatal error: Uncaught Error: Call to a member function isEmail() on null in C:wamp64wwwNew folder (5)vendorindex.php:20 Stack trace: #0 {main} thrown in C:wamp64wwwNew folder (5)vendorindex.php on line 20

please give solution @iyogeshkoli:disqus

U̷b̷a̷d̷a̷ M̷h̷i̷m̷i̷d̷
Guest
U̷b̷a̷d̷a̷ M̷h̷i̷m̷i̷d̷

What is the problem?

Warning: mysqli::__construct(): (HY000/1045): Access denied for user ‘DATABASE-USER’@’localhost’ (using password: YES) in C:xampphtdocsuserscriptdatabase_connection.php on line 16
Connection failed: Access denied for user ‘DATABASE-USER’@’localhost’ (using password: YES)

help me please

Yogesh Koli
Guest

@ubadamhimid:disqus – You have a problem in database connectivity, make sure to provide correct username and password.

Rabi Ulrehmat
Guest
Rabi Ulrehmat

Please tell me fast how can I remove these errors?????????????????????????????????????

Rabi Ulrehmat
Guest
Rabi Ulrehmat

Notice: Undefined variable: app in C:xampphtdocsmindex.php on line 20

Fatal error: Call to a member function isEmail() on null in C:xampphtdocsmindex.php on line 20

bilal
Guest
bilal

Notice: Undefined variable: app in C:xampphtdocsTCSNew folder (15)index.php on line 20

Fatal error: Call to a member function isEmail() on null in C:xampphtdocsTCSNew folder (15)index.php on line 20

U̷b̷a̷d̷a̷ M̷h̷i̷m̷i̷d̷
Guest
U̷b̷a̷d̷a̷ M̷h̷i̷m̷i̷d̷

What is the problem?

Warning: mysqli::__construct(): (HY000/1045): Access denied for user ‘DATABASE-USER’@’localhost’ (using password: YES) in C:xampphtdocsuserscriptdatabase_connection.php on line 16
Connection failed: Access denied for user ‘DATABASE-USER’@’localhost’ (using password: YES)

help me please

Amit pampaniya
Guest
Amit pampaniya

same error getting

Ingus Graholskis
Guest
Ingus Graholskis

is this script secure to sql injection?

Yogesh Koli
Guest

Yes, for sure.

Ingus Graholskis
Guest
Ingus Graholskis

Looking good!Is it also validated and sanitized ?Also do you have script that includes forgot password also?

Yogesh Koli
Guest

It will be there in upcoming tutorial.

Damien Norris
Guest
Damien Norris

Works perfectly, thank you!

Yogesh Koli
Guest

Great..!

Damien Norris
Guest
Damien Norris

I lied, PayPal not working or am I missing something in my dashboard setup?

Yogesh Koli
Guest

You may be have missing something on your setup, as above script is tested and works successfully.. you can checkout the live demo to make sure.. try to debug your script line by line and check if something is missing or wrong.

U̷b̷a̷d̷a̷ M̷h̷i̷m̷i̷d̷
Guest
U̷b̷a̷d̷a̷ M̷h̷i̷m̷i̷d̷

What is the problem??

Warning: mysqli::__construct(): (HY000/1045): Access denied for user ‘DATABASE-USER’@’localhost’ (using password: YES) in C:xampphtdocsuserscriptdatabase_connection.php on line 16
Connection failed: Access denied for user ‘DATABASE-USER’@’localhost’ (using password: YES)

help me please

U̷b̷a̷d̷a̷ M̷h̷i̷m̷i̷d̷
Guest
U̷b̷a̷d̷a̷ M̷h̷i̷m̷i̷d̷

What is the problem???

Warning: mysqli::__construct(): (HY000/1045): Access denied for user ‘DATABASE-USER’@’localhost’ (using password: YES) in C:xampphtdocsuserscriptdatabase_connection.php on line 16
Connection failed: Access denied for user ‘DATABASE-USER’@’localhost’ (using password: YES)

help me please

Pîrvan Marian
Guest
Pîrvan Marian

This don’t work for me 🙁

Yogesh Koli
Guest

What’s the issue?

U̷b̷a̷d̷a̷ M̷h̷i̷m̷i̷d̷
Guest
U̷b̷a̷d̷a̷ M̷h̷i̷m̷i̷d̷

What is the problem????

Warning: mysqli::__construct(): (HY000/1045): Access denied for user ‘DATABASE-USER’@’localhost’ (using password: YES) in C:xampphtdocsuserscriptdatabase_connection.php on line 16
Connection failed: Access denied for user ‘DATABASE-USER’@’localhost’ (using password: YES)

help me please

Maria Quintero
Guest
Maria Quintero

Hi, I like so much your post, can you upload the project, I can´t run 🙁

Tutorials You May Like!

- Advertisement -