User Registration with Email Verification in php

Hey Technoz, Hope you are doing Great! We have seen many websites uses email verification technique to avoid spam mails. In this post, we’ll also discuss about this topic. When the user registration or sign up is done, the verification email is sent to his/her email (using phpmailer library) from which he/she can verify the account.

As we are creating email verification using phpmailer library, I recommend you to go through following tutorial first to see how phpmailer works.

Send email with phpmailer library

1. Creating Database

As we have to do user registration, we have to create the database to store the values entered by user. Use the following query to create the database, table and required fields in it.

The above query will create a new table ‘users’ with fields id, name, password, email, verification. Furthermore, The first four fields will store data entered by user, while last field will have default value as ‘pending’. So, user registration will be pending at start.

2. Making connection with database

Create the following file config.php for connection with database.

3. Download phpMailer library

After user registration, we have to do email verification. We are using phpmailer library to send email as it is easy to use and has guaranteed email delivery. Download it from this link.

After downloading, extract the zip and put the ‘phpmailer’ folder in root directory of your project.

4. Creating User Registration form

Now, we’ll start coding our actual form for user registration. Lets create file index.php and paste the following php code in it.

Let me explain the above file. The file contains a simple form which takes input as name, email and password. After submitting a form, the code checks whether the user already exists in database. If so, it shows error and stops. Besides that, If there is new entry, the new user entry is created in database and simultaneously, the email is sent to the entered email address using phpmailer library. Do not forget to change the following credentials with yours:

Lets see the index page in the image below

User Registration with Email Verification in php

Have a look below of received email

User Registration with Email Verification in php

The $message variable in the index file contains all the well-formatted email body which is to be sent to the recipient. We also have to activate the user’s account after clicking on verify email button in an email. We have provided a link to a button, which calls the file, which contains the code for account activation. Note the line below stated in index file:

The link is generates using id of the user. To maintain the security, we have encoded the id using base64_encode() function.

5. Email Verification using phpmailer library

To activate the account, we have to code file activate.php as follows.

The above file will get the ‘id’ of the user, decrypts it using intval() function, and then the record matching with the id is updated. (value of ‘verification’ filed is change to ‘verified’). You can see the updated value in database. If user again clicked on the activation button, the message appears “Your account has already been activated”.

Thanks for watching this tutorial. Please subscribe to our newsletter to catch such knowledgeable and useful tutorials.

Download Source Code

If you have any problems implementing above tutorial, please comment below. Alternatively, you can always download source code free of cost from below link.

12 thoughts on “User Registration with Email Verification in php

    1. E
      Ezenwa Hopekell says:

      Check if your local server is running, or if you have any server at all, if you don’t, get Xampp, Lamp or WAMP, and install on your PC

  1. Flexx says:

    Thanks for sharing this

    Very nice script but I get a error

    End well this error

    Connection failed: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known

    What can I do to fix this ??

      1. Flexx says:

        I have change a lot in the files so I don’t know where I must check it so I will start over again no big deal.

        Thanks for you fast reaply.

        If I have more questions I will let you know.

Leave a Reply

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