Email Verification With OTP-One Time Password


 How to integrate OTP(One Time Password) in PHP form

Hello friends ajj me apko btaunga ki ham simple email otp verification script kaise bana sakte hai.

App demo niche diye gye link se check kar sakte ho;


Demo-    OTP Email Verification Script





Index2.php

<center><form action="mail.php" method="post">

Name:<input type="text" name="firstname">


Email:<input type="text" name="username">

Phone:<input type="text" name="lastname">


<input type="submit" name="submit" value="submit">

</form>

</center>

Index2.php file ki help se hamne front end ke liye ek form bnaya hai jiska use kerke user apne information,signup karega






mail.php




<center>

<?php

session_start();

$code=rand(22222,99999);

$_SESSION["code"]=$code;

$username=$_POST['username'];

$firstname=$_POST['firstname'];

$lastname=$_POST['lastname'];

$_SESSION["username"]=$username;

$_SESSION["firstname"]=$firstname;

$_SESSION["lastname"]=$lastname;

require 'PHPMailer-master/PHPMailerAutoload.php';



$mail = new PHPMailer();



  //Enable SMTP debugging.

  $mail->SMTPDebug = 0;

  //Set PHPMailer to use SMTP.

  $mail->isSMTP();

  //Set SMTP host name

  $mail->Host = "smtp.gmail.com";

  $mail->SMTPOptions = array(

                    'ssl' => array(

                        'verify_peer' => false,

                        'verify_peer_name' => false,

                        'allow_self_signed' => true

                    )

                );

  //Set this to true if SMTP host requires authentication to send email

  $mail->SMTPAuth = TRUE;

  //Provide username and password

  $mail->Username = "yourgmailidhere@gmail.com";

  $mail->Password = "yourgmailpassword";

  //If SMTP requires TLS encryption then set it

  $mail->SMTPSecure = "false";

  $mail->Port = 587;

  //Set TCP port to connect to



  $mail->From = "bhupenderkashyap122@gmail.com";

  $mail->FromName = "jignesh";



  $mail->addAddress($_POST["username"]);



  $mail->isHTML(true);

$mail->addAttachment('lop.jpg', 'lop1.jpg'); //set new name





  $mail->Subject = "test mail";

  $mail->Body = "<i>this is your password:</i>".$code;

  $mail->AltBody = "This is the plain text version of the email content";

  if(!$mail->send())

  {

   echo "Mailer Error: " . $mail->ErrorInfo;

  }

  else

  {

   echo "<h1>OTP has been send succesfully in Your email Id please checkout </h1>";



   ?>

  

   <form action="mail2.php" method="post">



<input name="captcha" type="text">





<input type="submit" name="submit" value="submit">

</form>

  

<?php





  }

  ?>









</center>



Yanha mail.php file me apko sirf apna gmail id ka username and password enter karna hai agar aap is script ko localhost par run karwa rahe ho.,mail.php file  me hamne rand function and php mailer library ka use kiya hai.yanha rand function random otp code generate karega  aur fir phpmailer library function ki madad se generate  hua random otp user ki email id par jayega..






  mail2.php


<center>

<br>

<br>

<br>

<br>

<br>





<?php

session_start();

$code="";


if(isset($_POST["captcha"])&&$_POST["captcha"]!=""&&$_SESSION["code"]==$_POST["captcha"])

{

echo $captcha=$_POST['captcha'];

echo "<br>";

echo "Correct Code Entered";



include 'insert.php';







session_destroy();

//Do you stuff

}

else

{

echo "Wrong Code Entered Try Again";







?>









<form action="mail2.php" method="post">



<input name="captcha" type="text">



<input type="submit" name="submit" value="submit">

</form>

















<?php } ?>



<a href="index2.php">Index</a>

</center>

<center>






</center>

mail2.php  file validate karegi ki user dawara enter kiya gya otp ,and userki email id par gya otp code same hai ya nhi,ye work yanha ham session ki madad se karege
.





insert.php


<?php





$conn=mysqli_connect("localhost","root","","google");

//enter your database detail Here



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

  echo "Your session is running " . $_SESSION['username'];



$username=$_SESSION['username'];

$firstname=$_SESSION['firstname'];

$lastname=$_SESSION['lastname'];

$sql = "INSERT INTO otp(username,firstname,lastname)

VALUES ('$username','$firstname','$lastname')";



mysqli_query($conn, $sql);



}



?>

Insert.php file ka kam hai ki agar user correct otp code  enter karta hai to user ke information firstname,lastname,and username ko databse me store kar lena.







Database code- -- phpMyAdmin SQL Dump

-- version 4.7.0

-- https://www.phpmyadmin.net/

--

-- Host: 127.0.0.1

-- Generation Time: Jun 19, 2018 at 08:11 AM

-- Server version: 10.1.26-MariaDB

-- PHP Version: 7.1.8



SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";

SET AUTOCOMMIT = 0;

START TRANSACTION;

SET time_zone = "+00:00";





/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;

/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;

/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;

/*!40101 SET NAMES utf8mb4 */;



--

-- Database: `google`

--



-- --------------------------------------------------------



--

-- Table structure for table `otp`

--



CREATE TABLE `otp` (

  `id` int(10) NOT NULL,

  `username` varchar(255) NOT NULL,

  `firstname` varchar(255) NOT NULL,

  `lastname` varchar(255) NOT NULL

) ENGINE=InnoDB DEFAULT CHARSET=latin1;



--

-- Indexes for dumped tables

--



--

-- Indexes for table `otp`

--

ALTER TABLE `otp`

  ADD PRIMARY KEY (`id`);



--

-- AUTO_INCREMENT for dumped tables

--



--

-- AUTO_INCREMENT for table `otp`

--

ALTER TABLE `otp`

  MODIFY `id` int(10) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;COMMIT;



/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;

/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;

/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

App complete CODE SCRIPT NICHE DIYE GYE link se download kar lijiye download karne ke bad apko is code me sirf mail.php file me apni gmail id and password enter karke save kar dena hai or sath he databse file ko mysql me import karna hai takki user ki information mysql databse me store ho sakte correct otp enter karne ke bad.

Download Full Email OTP Verification Script

Thank You

Team Gajabwap Web In Hindi


Previous
Next Post »

3 comments

Click here for comments
Unknown
admin
4 September 2019 at 23:12 ×

maine sare file ka code likh liya bana bhi liya aur PHPMailer file bhi hai mail.php me email and password apna add ki database bhi hai but error aa raha hai ye Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

Reply
avatar
gajabwap
admin
4 September 2019 at 23:50 ×

Bro ye code sirf web hosting me work krega pehle aap web hosting me sare code files upload kro wanha ye perfectly work krega

Reply
avatar
Unknown
admin
20 March 2021 at 01:13 ×

Today we will Discuss One interesting Topic OTP Bypass ! How hackers able to Bypass OTP Schema On Web Or Mobile based application. As You know A one-time password (OTP) is an automatically generated numeric or alphanumeric string of characters that authenticates the user for a single transaction or login session.

Reply
avatar
Thanks for your comment