DEV Community

Kenny Lenver
Kenny Lenver

Posted on • Originally published at computerinhindi.info

PHP Example For Send Otp

Send OTP To Mobile Number In PHP

<!DOCTYPE html>


How to Implement OTP SMS Mobile Verification in PHP with TextLocal





Mobile Number Verification
placeholder="Enter the 10 digit mobile">

onClick="sendOTP();">


HTML Form For Submit OTP For Verification In PHP

OTP is sent to Your Mobile Number

jQuery And Ajax Script For Sending OTP In PHP

function sendOTP() {
$(".error").html("").hide();
var number = $("#mobile").val();
if (number.length == 10 && number != null) {
var input = {
"mobile_number" : number,
"action" : "send_otp"
};
$.ajax({
url : 'controller.php',
type : 'POST',
data : input,
success : function(response) {
$(".container").html(response);
}
});
} else {
$(".error").html('Please enter a valid number!')
$(".error").show();
}
}

function verifyOTP() {
$(".error").html("").hide();
$(".success").html("").hide();
var otp = $("#mobileOtp").val();
var input = {
"otp" : otp,
"action" : "verify_otp"
};
if (otp.length == 6 && otp != null) {
$.ajax({
url : 'controller.php',
type : 'POST',
dataType : "json",
data : input,
success : function(response) {
$("." + response.type).html(response.message)
$("." + response.type).show();
},
error : function() {
alert("ss");
}
});
} else {
$(".error").html('You have entered wrong OTP.')
$(".error").show();
}
}

Controller.php File For mobile verification code in PHP

<?php
session_start();
error_reporting(E_ALL & ~ E_NOTICE);
require ('textlocal.class.php');

class Controller
{
function __construct() {
$this->processMobileVerification();
}
function processMobileVerification()
{
switch ($_POST["action"]) {
case "send_otp":

            $mobile_number = $_POST['mobile_number'];

            $apiKey = urlencode('YOUR_API_KEY');
            $Textlocal = new Textlocal(false, false, $apiKey);

            $numbers = array(
                $mobile_number
            );
            $sender = 'PHPPOT';
            $otp = rand(100000, 999999);
            $_SESSION['session_otp'] = $otp;
            $message = "Your One Time Password is " . $otp;

            try{
                $response = $Textlocal->sendSms($numbers, $message, $sender);
                require_once ("verification-form.php");
                exit();
            }catch(Exception $e){
                die('Error: '.$e->getMessage());
            }
            break;

        case "verify_otp":
            $otp = $_POST['otp'];

            if ($otp == $_SESSION['session_otp']) {
                unset($_SESSION['session_otp']);
                echo json_encode(array("type"=>"success", "message"=>"Your mobile number is verified!"));
            } else {
                echo json_encode(array("type"=>"error", "message"=>"Mobile number verification failed"));
            }
            break;
    }
}

}
$controller = new Controller();
?>
See Here

Top comments (2)

Collapse
 
nathandaly profile image
Nathan Daly

Don't forget to use Markdown to make code posts more readable.

Collapse
 
kennylenver profile image
Kenny Lenver

thanks you sir next time i'll keep in my mind