Skip to navigation Skip to main content
  • English
  • Country
  • Deutsch
  • French
  • Requires WPML plugin
  • United States (USD)
  • Deutschland (EUR)
  • Japan (JPY)
ADD ANYTHING HERE OR JUST REMOVE IT…
  • Newsletter
  • Contact Us
  • FAQs
  • Html
  • Css
  • Javascript
  • PHP
  • Jquery
Menu
Browse Categories
  • Instagram profile
  • New Collection
  • Woman Dress
  • Contact Us
  • Latest News
  • Purchase Theme
  • Blog
    • Theme elements
      • Alternative
      • Small images
      • Blog chess
      • Masonry grid
      • Meta on image
      • Infinit scrolling Features
      • Blog flat
      • Default flat
      • Blog mask
    • Theme elements Examples
      • Post example #1
      • Post example #2
      • Post example #3
      • Post example #4
      • Post example #5
      • Post example #6
      • Post example #7
      • Post example #8
    Recent Posts
    • display-popup-on-page-load
      Display popup on page load
      August 20, 2026 1 Comment
    • How to Create a Background Video for Header
      February 1, 2022 1 Comment
    • 3D Animation Flipping Image and Text Using Only Css
      February 1, 2022 1 Comment
  • Shop Pages
    • Shop layouts
      • Filters area
      • AJAX Shop
      • Hidden sidebar Hot
      • No page heading
      • Small categories menu
      • Products list view
      • With background
      • Category description
      • Header overlap
      • Infinit scrolling
      • Load more button

    Advanced Variable products with swatches

    Products variations colors and images without any additional plugins.

    View More
  • Product Loop
    • Hover design Effects
      • All info on hover
      • Icons & Add to cart
      • Icons on hover
      • Quick shop
      • Full info on image
      • Button on image
      • Summary on hover
      • Standard button
      • Tiled hover
      • Full width button
      • Additional information
    • Products styles
      • Even product grid
      • Products color scheme
      • Bordered grid style outside
      • Bordered grid style inside
      • Products background New
      • Products shadow
      • Show empty star rating
      • Show SKU
      • Stock status position
  • Single Product
    • Page design
      • Default
      • Centered
      • Sticky description
      • With shadow
      • With background
      • Accordion tabs New
      • Accordion in content
      • With sidebar
      • Sidebar full-height
      • Extra content #1
      • Extra content #2 Hot
    • Product image
      • Thumbnails left
      • Thumbnails bottom
      • Without thumbnails
      • Sticky images
      • One column
      • Two columns
      • Combined grid
      • Carousel (2 columns)
      • Images full-width
      • Images full-width (container)
      • Zoom image
    • Product types
      • Simple product
      • Grouped product
  • Features
    • Configuring settings
      • Variations swatches New
      • Catalog mode
      • Login to see prices
      • Cookies law info
      • Shop sidebar widgets collapse
      • Mobile bottom navbar
      • Age verification
      • Variation on shop page #1
      • Variation on shop page #2 New
    • Product features Unlimited
      • All images on shop page
      • Pagination in main gallery
      • Size guides
      • 360° product viewer
      • Full width product page
      • Quantity input on shop page
      • Custom product tabs
      • Show brand on product loop
    • Extra features
      • Sticky add to cart
      • Buy now button
      • Visitor counter
      • Custom product label
  • SPECIAL OFFER
  • PURCHASE THEME

Blog

Blog
Posted by author-avatar Sandilyan
August 22, 2026
On March 18, 2019
0
Hi now i will tell you how to create login system using mobile otp. First you need to purchase sms gateway to send otp via sms to the registered mobile number. OTP means one time password, it is valid only one time after login using that otp, that otp number will expired this is the concept of the otp, Now we will see how to do this with the help of php and mysql.

First you should purchase sms gateway various types sms service providers available through the world. you will purchase from the serveice providers, they will share you api key and sender id using that only you can able to send the sms to the mobile number. Here i am used smsgatewayhub service provider.

sms-otp-login-system-using-php

Now you will create database in backend and then create two different table one is otp_expiry table it is used to store the randomly generated otp in the database to use for login and another one table is registered_users, it will used to store the mobile number in database i already stored the mobile number manually you can store this mobile number using signup form.


Step1: Create Database

Create database in your backend, Here i am using database name is ” test” 


Step2: Create table with this name “otp_expiry”


create table name here i am using otp_expiry to store and alter the otp, Use below sql code to create the table.

CREATE TABLE `otp_expiry` (
  `otp` varchar(10) NOT NULL,
  `is_expired` int(11) NOT NULL,
  `create_at` datetime NOT NULL,
  `id` int(12) NOT NULL,
  `newid` int(12) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (newid)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Step3: Create table with this name “registered_users”


Create table name here i am using registered_users to store the registered user mobile number here i directly inserted some mobile number in database , you may include the data using registration form, Use the below sql code to create registration user

CREATE TABLE `registered_users` (
  `id` int(11) NOT NULL,
  `mobile` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Below is the full opt login system code is available you can download this from by clicking the download button there i also included the database file. I already used this code in my project it works 100% . You can download and use this code to your project, you need to modify the code. you just replace your api key and sender id then the url when you purchase the sms gateway to the same service provider or you can use the php integration part from some other service providers.

OTP Full form code:

<?php
$success = “”;
$error_message = “”;
$conn = mysqli_connect(“localhost”,”root”,””,”test”);
if(!empty($_POST[“submit_email”])) {
$result = mysqli_query($conn,”SELECT * FROM registered_users WHERE mobile='” . $_POST[“mobile”] . “‘”);
$count  = mysqli_num_rows($result);
if($count>0) {
// generate OTP
$otp = rand(100000,999999);
// Send OTP

$apikey = “YOUR API KEY“;
$apisender = “SENDER ID“;
$msg =”Your OTP for Login $otp”;
$num = $_POST[“mobile”];    // MULTIPLE NUMBER VARIABLE PUT HERE…!               

$ms = rawurlencode($msg);   //This for encode your message content                 

$url = ‘https://www.smsgatewayhub.com/api/mt/SendSMS?APIKey=’.$apikey.’&senderid=’.$apisender.’&channel=2&DCS=0&flashsms=0&number=’.$num.’&text=’.$ms.’&route=1′;
$mail_status=1;                 
//echo $url;
$ch=curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,””);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,2);
$data = curl_exec($ch);
echo ‘<br/><br/>’;
/* result of API call*/

if($mail_status == 1) {
$result = mysqli_query($conn,”INSERT INTO otp_expiry(otp,is_expired,create_at) VALUES (‘” . $otp . “‘, 0, ‘” . date(“Y-m-d H:i:s”). “‘)”);
$current_id = mysqli_insert_id($conn);
if(!empty($current_id)) {
$success=1;
}
}
} else {
$error_message = “Mobile not exists!”;
}
}
if(!empty($_POST[“submit_otp”])) {
$result = mysqli_query($conn,”SELECT * FROM otp_expiry WHERE otp='” . $_POST[“otp”] . “‘ AND is_expired!=1 AND NOW() <= DATE_ADD(create_at, INTERVAL 24 HOUR)”);
$count  = mysqli_num_rows($result);
if(!empty($count)) {
$result = mysqli_query($conn,”UPDATE otp_expiry SET is_expired = 1 WHERE otp = ‘” . $_POST[“otp”] . “‘”);
$success = 2;
} else {
$success =1;
$error_message = “Invalid OTP!”;
}
}
?>
<html>
<head>
<title>User Login</title>
<style>
body{
background-color: #4CAF50;
}
.sanloginpanel {
border: #ffffff 1px solid;
background: #ffffff;
border-radius: 4px;
max-width: 480px;
padding: 42px 30px 46px;
text-align: center;
margin: 150px auto;
}
.tableheader { font-size: 20px; }
.tablerow { padding:20px; }
.error_message {
color: #b12d2d;
background: #ffb5b5;
border: #c76969 1px solid;
}
.message {
width: 100%;
max-width: 300px;
padding: 10px 30px;
border-radius: 4px;
margin-bottom: 5px; 
}
.login-input {
border: #4caf50 1px solid;
padding: 10px 20px;
border-radius:4px;
}
.btnSubmit {
padding: 10px 20px;
background: #2c7ac5;
border: #d1e8ff 1px solid;
color: #FFF;
border-radius:4px;
}
</style>
</head>
<body>
<?php
if(!empty($error_message)) {
?>
<div class=”message error_message”><?php echo $error_message; ?></div>
<?php
}
?>

<form name=”frmUser” method=”post” action=””>
<div class=”sanloginpanel”>
<?php
if($success == 1) { ?>
<div class=”tableheader”>Enter OTP</div>
<p style=”color:#31ab00;”>Check your Mobile for the OTP</p>

<div class=”tablerow”>
<input type=”text” name=”otp” placeholder=”One Time Password” class=”login-input” required>
</div>
<div class=”tableheader”><input type=”submit” name=”submit_otp” value=”Submit” class=”btnSubmit”></div>
<?php
} else if ($success == 2) {
$result = mysqli_query($conn,”SELECT * FROM otp_expiry WHERE otp='” . $_POST[“otp”] . “‘”);

$count=mysqli_num_rows($result);
$row=mysqli_fetch_array($result);

if($count > 0){
session_start();
$_SESSION[‘newid’]=$row[‘newid’];

header(‘Location: securepage.php’);
}

?>
<?php
}
else {
?>
<div class=”tableheader”>Enter Your Mobile Number to Login</div>
<div class=”tablerow”><input type=”text” name=”mobile” placeholder=”Enter Mobile Number” class=”login-input” required></div>
<div class=”tableheader”><input type=”submit” name=”submit_email” value=”Submit” class=”btnSubmit”></div>
<?php
}
?>
</div>
</form>
</body>
</html>

Here i generate the 6 digit otp code you can also generate the 4digit and whatever you want, just modify the rand(100000,999999) this code. I hope this opt form will really helpful and also i provided the screenshots below to refer

Screenshots:

mobile otp loginmysqlPHPsms otp login
Newer
Marquee text with close button using jquery
Back to list
Older Increment Decrements certain fixed amount of text box value multiply based on select box value
Close
Categories
  • Blog
Recent Posts
  • display-popup-on-page-load
    Display popup on page load
    August 20, 2026 1 Comment
  • How to Create a Background Video for Header
    February 1, 2022 1 Comment
  • 3D Animation Flipping Image and Text Using Only Css
    February 1, 2022 1 Comment
Recent Comments
    Facebook X-twitter Youtube Linkedin
    Copyright 2026 © SanWebCorner
    • Menu
    • Categories
    • Instagram profile
    • New Collection
    • Woman Dress
    • Contact Us
    • Latest News
    • Purchase Theme
    • Blog
    • Compare
    Shop
    Sidebar