How to Build Login System with OTP using PHP & MySQL
Login with OTP (One Time Password) is a typical performance to make person login more safe. The One Time Password generated randomly and ship to person e mail tackle or cell to confirm OTP code to full login.
So if you’re occupied with implementing safe login with PHP using OTP, you then’re right here at proper place. In this tutorial, you’ll learn the way to implement safe login in PHP with OTP code.
We will cowl this tutorial in straightforward steps to implement safe login with OTP using PHP and MySQL. When login profitable then OTP code ship to e mail tackle and stay legitimate for one hour, after that code will probably be expired. The person login accomplished after OTP code verified. You also can create an entire User Management System with PHP & MySQL to handle every little thing associated to users with admin panel.

So let’s begin implementing safe login with OTP using PHP and MySQL. Before we start, have a look on information construction for this instance.
- index.php
- confirm.php
- welcome.php
- logout.php
How to Create MySQL Database Table
As we’ll implement safe login with OTP with PHP and MySQL, so we’ll create MySQL database desk members to retailer person login particulars.
CREATE TABLE `members` ( `id` int(11) NOT NULL, `username` varchar(250) NOT NULL, `e mail` varchar(250) NOT NULL, `password` varchar(250) NOT NULL, `authtoken` varchar(250) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
We may also create MySQL database desk authentication to retailer login OTP particulars to validate OTP code.
CREATE TABLE `authentication` ( `id` int(11) NOT NULL, `otp` varchar(10) NOT NULL, `expired` int(11) NOT NULL, `created` datetime NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
How to Create Login Form
First in index.php, we’ll create person login kind to implement login performance.
<div class="col-md-6"> <div class="panel panel-info" > <div class="panel-heading"> <div class="panel-title">Sign In</div> </div> <div model="padding-top:30px" class="panel-body" > <?php if ($errorMessage != '') ?> <div id="login-alert" class="alert alert-danger col-sm-12"><?php echo $errorMessage; ?></div> <?php ?> <kind id="loginform" class="form-horizontal" position="form" methodology="POST" motion=""> <div model="margin-bottom: 25px" class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span> <enter sort="text" class="form-control" id="email" title="email" placeholder="email"> </div> <div model="margin-bottom: 25px" class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span> <enter sort="password" class="form-control" id="loginPass" title="loginPass" placeholder="password"> </div> <div model="margin-top:10px" class="form-group"> <div class="col-sm-12 controls"> <enter sort="submit" title="login" worth="Login" class="btn btn-success"> </div> </div> </kind> </div> </div> </div>
How to Implement Login and Send OTP
We will implement person login performance on login kind submit. If person login profitable, then ship OTP code to person e mail tackle and insert OTP particulars to database desk authentication. Also redirect to OTP code confirm web page for finishing person login.
if(!empty($_POST["login"]) && $_POST["email"]!=''&& $_POST["loginPass"]!='') $e mail = $_POST['email']; $password = $_POST['loginPass']; $password = md5($password); $sqlQuery = "SELECT username FROM members WHERE email='".$e mail."' AND password='".$password."'"; $resultSet = mysqli_query($conn, $sqlQuery) or die("database error:". mysqli_error($conn)); $isValidLogin = mysqli_num_rows($resultSet); if($isValidLogin) $userDetails = mysqli_fetch_assoc($resultSet); $_SESSION["user"] = $userDetails['username']; $otp = rand(100000, 999999); $headers = "MIME-Version: 1.0" . "rn"; $headers .= "Content-type:text/html;charset=UTF-8" . "rn"; $headers .= 'From: help@webdamn.com' . "rn"; $messageBody = "One Time Password for login authentication is:
” . $otp; $messageBody = wordwrap($messageBody,70); $subject = “OTP to Login”; $mailStatus = mail($email, $subject, $messageBody, $headers); if($mailStatus == 1) $insertQuery = “INSERT INTO authentication(otp, expired, created) VALUES (‘”.$otp.”‘, 0, ‘”.date(“Y-m-d H:i:s”).”‘)”; $result = mysqli_query($conn, $insertQuery); $insertID = mysqli_insert_id($conn); if(!empty($insertID)) header(“Location:confirm.php”); else $errorMessage = “Invalid login!”; else if(!empty($_POST[“e mail”])) $errorMessage = “Enter Both person and password!”;
How to Create Verify OTP Form
We will create OTP confirm kind in confirm.php to very OTP for finishing person login.
<div class="col-md-6"> <div class="panel panel-info" > <div class="panel-heading"> <div class="panel-title">Enter OTP</div> </div> <div model="padding-top:30px" class="panel-body" > <?php if ($errorMessage != '') ?> <div id="login-alert" class="alert alert-danger col-sm-12"><?php echo $errorMessage; ?></div> <?php ?> <kind id="authenticateform" class="form-horizontal" position="form" methodology="POST" motion=""> <div model="margin-bottom: 25px" class="input-group"> <label class="text-success">Check your e mail for OTP</label> <enter sort="text" class="form-control" id="otp" title="otp" placeholder="One Time Password"> </div> <div model="margin-top:10px" class="form-group"> <div class="col-sm-12 controls"> <enter sort="submit" title="authenticate" worth="Submit" class="btn btn-success"> </div> </div> </kind> </div> </div> </div>
How to Implement OTP Verification
Now we’ll implement OTP confirm on confirm kind submit. We will examine for OTP if it is not expired and not more than one hour outdated then validate OTP in any other case show invalid OTP message to person to try login once more. If OTP code is legitimate and verified then replace it as expired after profitable login authentication.
if(!empty($_POST["authenticate"]) && $_POST["otp"]!='') { $sqlQuery = "SELECT * FROM authentication WHERE otp='". $_POST["otp"]."' AND expired!=1 AND NOW()
Download otp Submit Form Code
You can view the dwell demo from the Demo hyperlink and can obtain the script from the Download hyperlink beneath.
Download
How to Build Login System with OTP using PHP & MySQL-My programming school