1. generateOTP($n)
Where $n is how many digits of OTP you want to send.
function generateOTP($n) {
$generat = "123456789";
$otp = "";
for ($i = 1; $i <= $n; $i++)
{
$otp .= substr($generat, (rand()%(strlen($generat))), 1);
}
return $otp;
}
2. send_sms_dip($mobile,$sms_body)
$mobile=To which number you want to sent otp
$sms_body="Dear Student your Dashboard login OTP is $opt ebluesys";
Where $otp is
$otp=generateOTP(6);
We are using SMSPacttown to send our OTP
user=YourUSER
password=YourPASS
senderid=YourSENDERID
You will get these credential only by signup into SMSPacttown
function send_sms_dip($mobile,$sms_body)
{
$sms_body=urlencode($sms_body);
$sms_url="http://login.pacttown.com/api/mt/SendSMS?user=YourUSER&password=YourPASS&senderid=YourSENDERID&channel=Trans&DCS=0&flashsms=0&number=91".$mobile."&text=".$sms_body."&route=2";
$ch = curl_init($sms_url);
$timeout = 500;
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_exec($ch);
}
<form name="form1" method="post" enctype="multipart/form-data" class="request-info clearfix">
<div id="sndotp">
<label>Enter Your Registered Mobile No </label>
<img id="loading-image" src="load-loading.gif" style="display:none;width:85px"/>
<span id="ms"></span>
<input type="text" name="sndpotp" id="sndpotp" style="width:150px">
<br>
<span class="btn btn-info" onclick="sendotp()"> Get OTP </span>
</div>
<div class="half-row" id="lgo2" style="display:none">
<label>Please Check Your Registered Mobile No & Enter 6 Digit Otp </label>
<input type="text" name="potp" id="potp" placeholder="******" onkeyup="check_otp()" style="width:150px"><br>
<button type="button" id="slog" class="btn btn-primary" onclick="check_form()"><span id="btnck">Log In</span></button>
</div>
</form>
Here we have created a Html form where has two part
Part 1 ....
<div id="sndotp">
<label>Enter Your Registered Mobile No </label>
<img id="loading-image" src="load-loading.gif" style="display:none;width:85px"/>
<span id="ms"></span>
<input type="text" name="sndpotp" id="sndpotp" style="width:150px">
<br>
<span class="btn btn-info" onclick="sendotp()"> Get OTP </span>
</div>
Clicking The Get OTP button here call the sendotp() function...
This function calls a ajax page.
<script>
function sendotp()
{
var mno=$("#sndpotp").val();
if(mno!="")
{
data_pass='mno='+mno;
$.ajax
({
type: 'POST',
url: 'ajax_send_otp.php',
data: data_pass,
beforeSend: function() {
$("#loading-image").show();
},
success: function(response)
{
$("#loading-image").hide();
$("#ms").html(response);
if(response=="done")
{
$("#sndotp").hide();
$("#lgo2").show();
}
},
error: function(jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
});
}
}
</script>
ajax_send_otp.php
<?php
$connection = mysqli_connect("localhost","youruser","yourpass","yourdatabase");
function generateOTP($n) {
$generat = "123456789";
$otp = "";
for ($i = 1; $i <= $n; $i++)
{
$otp .= substr($generat, (rand()%(strlen($generat))), 1);
}
return $otp;
}
function send_sms_dip($mobile,$sms_body)
{
$sms_body=urlencode($sms_body);
$sms_url="http://login.pacttown.com/api/mt/SendSMS?user=YourUSER&password=YourPASS&senderid=YourSENDERID&channel=Trans&DCS=0&flashsms=0&number=91".$mobile."&text=".$sms_body."&route=2";
$ch = curl_init($sms_url);
$timeout = 500;
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_exec($ch);
}
if(isset($_REQUEST['mno']))
{
$mobileno=$_REQUEST['mno'];
$sql_check_mobileno=mysqli_fetch_array(mysqli_query($connection,"select 1 from a_students_data where field_id=7 and field_value='$mobileno'"));
if($sql_check_mobileno)
{
$sql_check_mobileno=mysqli_fetch_array(mysqli_query($connection,"select student_id from a_students_data where field_id=7 and field_value='$mobileno'"));
$student_id=$sql_check_mobileno['student_id'];
$check_new_old=mysqli_fetch_array(mysqli_query($connection,"select 1 from student_otp where student_id=$student_id"));
if($check_new_old)
{
$opt=generateOTP(6);
$sms_body="Dear Student your Dashboard login OTP is $opt ebluesys";
send_sms_dip($mobileno,$sms_body);
$tm=time();
mysqli_query($connection,"update student_otp set otp='$opt',send_time=$tm where student_id=$student_id");
}else{
$opt=generateOTP(6);
$sms_body="Dear Student your Dashboard login OTP is $opt ebluesys";
send_sms_dip($mobileno,$sms_body);
$tm=time();
mysqli_query($connection,"insert into student_otp set student_id=$student_id,mobile_no='$mobileno',otp='$opt',send_time=$tm");
}
echo "done";
}else{
echo "<span style='color:red'> Enter Valid Mobile No </span>";
}
}
?>
Part 2...
<div class="half-row" id="lgo2" style="display:none">
<label>Please Check Your Registered Mobile No & Enter 6 Digit Otp </label>
<input type="text" name="potp" id="potp" placeholder="******" onkeyup="check_otp()" style="width:150px">
<br>
<button type="button" id="slog" class="btn btn-primary" onclick="check_form()"><span id="btnck">Log In</span></button>
</div>
We are checking OTP Valid or Not by using chack_otp().
<script>
function check_otp()
{
var mno=$("#sndpotp").val();
var otp=$("#potp").val();
if(mno!="")
{
data_pass='mno='+mno+'&otp='+otp;
$.ajax
({
type: 'POST',
url: 'ajax_send_otp_check.php',
data: data_pass,
success: function(response)
{
if(response=="done")
{
//$("#slog").show();
$("#btnck").html("Log In");
}else{
$("#btnck").html('<span style="color:red"> Enter Valid Otp</span>');
//$("#slog").hide();
}
},
error: function(jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
});
}
}
</script>
ajax_send_otp_check.php
<?php
$connection = mysqli_connect("localhost","youruser","yourpass","yourdatabase");
if(isset($_REQUEST['mno']))
{
$mobileno=$_REQUEST['mno'];
$otp=$_REQUEST['otp'];
$check_otp=mysqli_fetch_array(mysqli_query($connection,"select 1 from student_otp where mobile_no='$mobileno' and otp='$otp'"));
if($check_otp)
{
echo "done";
}else{
echo "sorry";
}
}
?>
<?php
if($h_action=="login")
{
$mobileno=$_POST['sndpotp'];
$otps=$_POST['potp'];
}
?>
Today we will learn how to verify email id while signup. it's very important for the real user. You ..
This is a common problem for those of you who are new to coding. While coding we often make some syn..
Warning: session start() [function.session-start]: Cannot send session cache limiter - headers alrea..
Before we use the PDO, we will know why we will use PDO.PDO Only supports object-oriented.It has man..
Deprecated: autoload() is deprecated, use spl autoload register() instead. How to replace this (my a..
getimagesize() is a PHP function that returns an array containing information about the size and typ..
Today we will know - How to login with otp in php. Here we will use php,mysql & ajax. Before tha..
Get the latest news and updates by signing up to our daily newsletter.We won't sell your email or spam you !