Ask Question

Menu

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


Here we are using 5 files for integrate OTP(One Time Password) in PHP form.

index.php

<!DOCTYPE html>
<html>
<title>Form</title>
<body>
<form action="process.php" method="post">
Name:<input class="input" type="text" placeholder="name" name="name" required><br><br>
Email:<input class="input" type="email" placeholder="email" name="email" required><br><br>
Phone:<input class="input" type="text" placeholder="phone" name="phone" required><br><br>
<button type="submit" name="btn-save">Submit</button>
</form>
</body>
</html>

process.php

<?php
session_start();
//Your authentication key
$authKey = "abgbsaxhbsadbasbhdjhsajhd";
//Multiple mobiles numbers separated by comma
$mobileNumber = $_POST["phone"];
//Sender ID,While using route4 sender id should be 6 characters long.
$senderId = "ABCDEF";
//Your message to send, Add URL encoding here.
$rndno=rand(1000, 9999);
$message = urlencode("otp number.".$rndno);
//Define route
$route = "route=4";
//Prepare you post parameters
$postData = array(
'authkey' => $authKey,
'mobiles' => $mobileNumber,
'message' => $message,
'sender' => $senderId,
'route' => $route
);
//API URL
$url="sms api";
// init the resource
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData
//,CURLOPT_FOLLOWLOCATION => true
));
//Ignore SSL certificate verification
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//get response
$output = curl_exec($ch);
//Print error if any
if(curl_errno($ch))
{
echo 'error:' . curl_error($ch);
}
curl_close($ch);
if(isset($_POST['btn-save']))
{
$_SESSION['name']=$_POST['name'];
$_SESSION['email']=$_POST['email'];
$_SESSION['phone']=$_POST['phone'];
$_SESSION['otp']=$rndno;
header( "Location: otp.php" ); } ?>

otp.php

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<title>Form</title>
<body>
<form action="otpprocess.php" method="post">
<input type="text" name="otpvalue"/>
<input type="submit" value="submit" />
</form>
</body>
</html>

otpprocess.php

<?php
session_start();
$url='127.0.0.1:3306';
$username = "root";
$password = "";
$dbname = "admin";
$conn = mysqli_connect($url, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$rno=$_SESSION['otp'];
$urno=$_POST['otpvalue'];
if(!strcmp($rno,$urno))
{
$name=$_SESSION['name'];
$email=$_SESSION['email'];
$phone=$_SESSION['phone'];
// Create connection
$sql = "INSERT INTO quote (name, email, phone)
VALUES ('$name', '$email', '$phone')";
if (mysqli_query($conn, $sql)) {
$authKey = "abcdefghijkakkkanhas";
$mobileNumber = $phone;
//Sender ID,While using route4 sender id should be 6 characters long.
$senderId = "ABCDEF";
//Your message to send, Add URL encoding here.
$message = urlencode("Thank u for register with us. we will get back to u shortly.");
//Define route
$route = "route=4";
//Prepare you post parameters
$postData = array(
'authkey' => $authKey,
'mobiles' => $mobileNumber,
'message' => $message,
'sender' => $senderId,
'route' => $route
);
//API URL
$url="sms api";
// init the resource
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData
//,CURLOPT_FOLLOWLOCATION => true
));
//Ignore SSL certificate verification
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//get response
$output = curl_exec($ch);
//Print error if any
if(curl_errno($ch))
{
echo 'error:' . curl_error($ch);
}
curl_close($ch);
header( "Location: sucess.php" );
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
return true;
}
else
{
echo "failure";
return false;
}
?>

success.php

<!DOCTYPE html>
<html>
<title>Sucess</title>
<body>
<p>Suceess</p>
</body>
</html>






Subscribe with us to get latest topic update






Choose a Language



Subscribe