how to generate and send otp using php?
how to generate and send otp using php?

As you all guyz know that security of user account is the critical problem in these days. So, the developer have to think about how they can secure their user account. This is can done by generating and sending OPT to the user phone number through SMS.

First understand what is OTP?

A One Time Password (OTP) is a unique code/token generated by our application, sent to a user via SMS text and then entered into our application login/signup flow for additional security.

  • People supply a mobile number when they join your service.
  • You generate a unique code and merge it into an SMS text and send to the user.
  • The user types the code into a form on your server and you verify the code matches.
  • You delete the code after a successful login or after a period of time.

One-Time Passwords are only effective for a fixed period of time and become invalid once the user logs in, making them exceptionally useful against spyware such as key logging programs.

One you have built One Time Passwords into your activation/signup flow a One Time Password will be texted every time a login attempt is made.  This flow will dramatically improve account security.

“This tutorial may be very useful for those who want to integrate OTP features in their project which is developed in PHP”.

So for this, we have to take the help of SMS gateway to send the OTP to the user Phone Number.

We are going to take help of txtlocal as our SMS gateway. All you have do is go to and there you need to create one account. after creating account. (you need to fill up some of your information and txtlocal will send you a verification email into your email and verify your account [watch the video for detail]). After completing this step, go to and scroll down, at the bottom of the page you will see Send SMS via PHP; click that link and copy the code from there and then edit as in the following manner or simply copy the below code but you need to provide your own hash key when you run this project.

Now write the following piece of code in any text editing software, i am using dream weaver cc. and saved this php file in .php extension in your localhost and run (Note: your php file will not execute in the same way you run your html file, because PHP is server side scripting language so you need to have any server application to run this file.  watch this video to configure site and testing server in dream weaver cc:  or read from here: click here)

<!doctype html>
$name=$_POST['name'];                                                                                      $username = "YOUR_USERNAME";
$hash = "YOUR_HASH_KEY";

// Config variables. Consult for more info.
$test = "0";

// Data for text message. This is the text message data.
$sender = "YOUR_SENDER_NAME"; // This is who the message appears to be from.
$numbers = $_POST['num']; // A single number or a comma-seperated list of numbers
$otp=mt_rand(100000,999999); //randrom number generation
setcookie("otp", $otp);
$message = "hey ".$name." your otp is ".$otp." send successfully";
// 612 chars or less
// A single number or a comma-seperated list of numbers
$message = urlencode($message);
$data = "username=".$username."&hash=".$hash."&message=".$message."&sender=".$sender."&numbers=".$numbers."&test=".$test;
$ch = curl_init('');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch); // This is the result from the API
echo ($result);
echo('otp send successfully');
echo("verify successfully");
echo("otp wrong");

<meta charset="utf-8">

<form method="post" action="otp.php">
<table align="center">
<td><input type="text" name="name" placeholder="enter your Name"></td>
<td>Valid Phone Number:</td>
<td><input type="text" name="num" placeholder="enter valid number"></td>
<td>send code</td>
<td><input type="submit" name="login" value="CLICK HERE"></td>

<td>Verify OTP:</td>
<td><input type="text" name="otp"></td>
<td><input type="submit" name="ver" value="Verify"></td>

May be after sending 10 OTP SMS your credit will be finished! but don’t be sad you can send more and more OTP SMS but you need to create another account with another email account in ! that’s it. you may not be that much happy to hear this (sending only 10 OTP SMS) but don’t forget you are able to send these 10 OTP SMS for free and also there is one line “To get something, you must lose something”. it means you have to spend some time in creating account in in order to send OTP SMS.

Finally, if you guyz think, this is awesome tutorial and help you in some extend please share this tutorials with your all friend so that they can know about this method of sending OTP SMS!

watch the video:




Please enter your comment!
Please enter your name here