Creating a Login Form with OTP Verification through Email in PHP Tutorial | Free Source Code Projects and Tutorials

Creating the interfaces

Next, create the login page of the application. Copy/Paste the code below and configure it the way you want the UI looks. Save the file as login.php.

Creating the php main class

The application that we are creating will be using an OOP Approach in writing the back-end scripts of the app. The class includes the database configuration and send mail function. Save the code file below as MainClass.php.

Demo video

There you go. You can now test the application on your end and check if it is working properly and achieves the goal I stated above. If there’s an error occurred on your end, review your codes with the codes I provided above. You can also download the source code I created for this tutorial. The download link/button is located after this article.

That ends this tutorial. I hope this will help you with what you are looking for and you’ll find it useful with your current or future projects.

Explore more on this website for more Tutorials and Free Source Codes.

Related Links:

Email otp verification using php in live server – geeksforgeeks

<!DOCTYPE html>

<html lang="en">







    $username="sign up";











            echo("failed to connect to database");









            echo("<script>alert('password not matches')</script>");





    "<script>alert('password length must be atleast 8')</script>");



                $query="insert into 1_user(username,email,password)


                $sql= "SELECT id,username, password FROM 1_user";

                $result= $con->query($sql);



                if(($result->num_rows)> 0){

                    while($row= $result->fetch_assoc()) {

                            " - username= ". $row["username"] .

                            " password= ". $row["password"] . "<br>";



























                        echo("mail send faild");




            "<script>alert('username  already taken')</script>");







    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible"content="IE=edge">

    <meta name="viewport"content=

        "width=device-width, initial-scale=1.0">


    <link rel="stylesheet"type="text/css"


    <!--  adding bootstrap  -->

    <link rel="stylesheet"href=


"sha384-Gn5384xqQ1aoWXA 058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"






    <script src=


"sha384-ApNbgh9B Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"



    <script src=


"sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5 76PVCmYl"



    <div class="nav-bar">

        <div class="title">

            <h3>welcome to my website</h3>





    <form class="form-register"


        <div class="form-group">


            <input type="text"class="form-control"





        <div class="form-group">


            <input type="email"class="form-control"




        <div class="form-group">


            <input type="password"class="form-control"




        <div class="form-group">


            <input type="password"





        <button type="submit"

            class="btn btn-primary btn-lg">



        <button type="button"

            class="btn btn-warning btn-lg"


            Already Registered




        $("#login-button").click(function() {






Generating the code

You can also generate the verification code yourself using the library.

Похожее:  php - Отправка смс с кодом при авторизации - Stack Overflow на русском

print TokenAuth6238::getTokenCodeDebug($secretkey,0);

Generating the qrcode for google authenticator

You can also generate the image that can be used by mobile device to configure authentication program

Getting started

Download and Install XAMPP as your virtual server in your local machine. Also, download Bootstrap and jQuery for the design of the application that we will be creating.


If you come across any issues please report them here


PHP Implementation of the OTP algorithm

Two factor authentication in PHP
I hope it could help you to make your applications more secure. Two factor authentication adds one more step into the authentication process and therefore provides a mechanism to provide more security for your systems.

Step 1

Open you google account’s settings.

Step 2

Navigate the page into the ‘security’ and click the link on turning on less secure apps.

Step 3

Toggle to allow less secure apps.


You need additional tool to send SMS. This module only takes care of the verification part.

Verification process

OTP verification is done in the following steps:

What is it?

This is a simple script written in php to verify One Time Password (OTP) without any database. You can read the blog post here to understand the technique and motivation.

Setting-up the xampp to send mail

First, locate your XAMPP’s php.ini file and open it with your favorite text editor such as notepad or sublime text. Next, in your text editor find the mail configuration by searching it using “[mail function]”. Then, follow the configuration shown in the image below.

Похожее:  Не могу войти в Дневник.ру - причины и что делать?

Next, setup the XAMPP’s sendmail configuration. Locate and open the sendmail.ini file in your text editor. Then, follow the configuration shown in the image below.

Note: Use your gmail credential of the account you want use to send mail.
1 Звезда2 Звезды3 Звезды4 Звезды5 Звезд (1 оценок, среднее: 4,00 из 5)

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *