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">

<?php

    session_start();

    $otp=$_SESSION["OTP"];

    if(isset($_SESSION["logged-in"])){

        header("Location:profile.php");

    }

    $username="sign up";

    $login_btn="Login";

    if(isset($_SESSION["username"])){

        $username=$_SESSION["username"];

        $login_btn="Logout";

    }

    if($_SERVER["REQUEST_METHOD"]=="POST"){

        $con=mysqli_connect('localhost',

            'database_username',

            'database_pass','database_name');

        if(!$con)

            echo("failed to connect to database");

        $username1=$_POST['username'];

        $prefix="_";

        $username=$prefix.$username1;

        $password=$_POST['Password'];

        $repassword=$_POST['RePassword'];

        $email1=$_POST['Email'];

        $email=strval($email1);

        if($password!=$repassword){

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

        }

        else{

            if(strlen($password)<8){

                echo(

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

            }

            else{

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

                        values('$username','$email','$password')";

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

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

                $username_already_exist=false;

                $email_already_exist=false;

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

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

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

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

                        if($row["username"]==$username){   

                            $username_already_exist=true;

                            break;

                        }

                        if($row["email"]==$email){   

                            $email_already_exist=true;

                            break;

                        }

                    }

                }

                if($username_already_exist==false){

                    $from="[email protected]";

                    $to=$email;

                    $subject="verify-account-otp";

                    $otp=rand(100000,999999);

                    $message=strval($otp);

                    $headers="From:".$from;

                    if(mail($to,$subject,$message,$headers)){

                        $_SESSION["username"]=$username;

                        $_SESSION["OTP"]=$otp;

                        $_SESSION["Email"]=$email;

                        $_SESSION["Password"]=$password;

                        $_SESSION["registration-going-on"]="1";

                        header("Location:verify-otp.php");

                    }

                    else

                        echo("mail send faild");

                }

                else{

                    echo(

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

                }

            }

        }

    }

?>

<head>

    <meta charset="UTF-8">

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

    <meta name="viewport"content=

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

    <title>Document</title>

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

        href="css/style.css"media="screen"/>

    <!--  adding bootstrap  -->

    <link rel="stylesheet"href=

        integrity=

"sha384-Gn5384xqQ1aoWXA 058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"

        crossorigin="anonymous">

        integrity=

"sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"

        crossorigin="anonymous">

    </script>

    <script src=

        integrity=

"sha384-ApNbgh9B Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"

        crossorigin="anonymous">

    </script>

    <script src=

        integrity=

"sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5 76PVCmYl"

        crossorigin="anonymous">

    </script>

    <div class="nav-bar">

        <div class="title">

            <h3>welcome to my website</h3>

        </div>

    </div>

</head>

<body>

    <form class="form-register"

        action="register.php"method="POST">

        <div class="form-group">

            <label>username</label>

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

                name="username"id="username"

                aria-describedby="emailHelp"

                placeholder="username"required>

        </div>

        <div class="form-group">

            <label>Email</label>

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

                name="Email"id="Email"

                placeholder="Email"required>

        </div>

        <div class="form-group">

            <label>Password</label>

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

                name="Password"id="Password"

                placeholder="Password"required>

        </div>

        <div class="form-group">

            <label>Password</label>

            <input type="password"

                class="form-control"name="RePassword"

                id="RePassword"placeholder="RePassword"

                required>

        </div>

        <button type="submit"

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

            Register

        </button>

        <button type="button"

            class="btn btn-warning btn-lg"

            id="login-button">

            Already Registered

        </button>

    </form>

    <script>

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

            window.location.replace("index.php");

        });

    </script>

</body>

</html>

Generating the code

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

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.

Issues

If you come across any issues please report them here

Phpotp

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.

Usage

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.
Похожее:  Пишем голосового ассистента на Python

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

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