A One Time Password (OTP) is a very popular way to use online transactions. It is used for real-time banking and monetary transactions. The following procedure is used by the OTP:

  1. Check email, phone number and user ID.
  2. You can change or reset user profile information.
  3. Real-time transaction authentication.
  4. Check the validation of the email and mobile number.

Create a One Time Password (OTP) in PHP

  1. Step 1

    Create the file otppass.php with the following code:
    1. <?php
    2. /**** ANANT ONE-TIME PASSWORD EXAMPLE ****/
    3. session_start(); //STARTING THE SESSION AND THE
    4. session_set_cookie_params(360);//SESSION EXPIRES IN 6 MINUTES
    5. // USERNAME AND PASSWORD ARRAYS
    6. $user = array(
    7. 'user1' => annat,
    8. 'scott' => tiger,
    9. ‘anat’ => xxxxxxx,
    10. );
    11. $phone = array(
    12. 'user1' => '+5353535333,
    13. 'scott' => '+44243535353,
    14. anat’ => '+23554444444,
    15. );
    16. // Login information for anant NG - SMS Gateway
    17. $anant_user = "admin";
    18. $anant_password = "abc123";
    19. $anant_url = "http://127.0.0.1:9501/api?";
    20. // Functions used to send the SMS message
    21. function httpRequest($url){
    22. $pattern = "/http...([0-9a-zA-Z-.]*).([0-9]*).(.*)/";
    23. preg_match($pattern,$url,$args);
    24. $in = "";
    25. $fp = fsockopen("$args[1]", $args[2], $errno, $errstr, 30);
    26. if (!$fp) {
    27. return("$errstr ($errno)");
    28. } else {
    29. $out = "GET /$args[3] HTTP/1.1\r\n";
    30. $out .= "Host: $args[1]:$args[2]\r\n";
    31. $out .= "User-agent: anant PHP client\r\n";
    32. $out .= "Accept: */*\r\n";
    33. $out .= "Connection: Close\r\n\r\n";
    34. fwrite($fp, $out);
    35. while (!feof($fp)) {
    36. $in.=fgets($fp, 128);
    37. }
    38. }
    39. fclose($fp);
    40. return($in);
    41. }
    42. function anantSend($phone, $msg, $debug=false){
    43. global $anant_user,$anant_password,$anant_url;
    44. $url = 'username='.$anant_user;
    45. $url.= '&password='.$anant_password;
    46. $url.= '&action=sendmessage';
    47. $url.= '&messagetype=SMS:TEXT';
    48. $url.= '&recipient='.urlencode($phone);
    49. $url.= '&messagedata='.urlencode($msg);
    50. $urltouse = $anant_url.$url;
    51. //if ($debug) { echo "Request: <br>$urltouse<br><br>"; }
    52. //Open the URL to send the message
    53. $response = httpRequest($urltouse);
    54. if ($debug) {
    55. echo "Response: <br><pre>".
    56. str_replace(array("<",">"),array("<",">"),$response).
    57. "</pre><br>"; }
    58. return($response);
    59. }
    60. //FUNCTION TO GENERATE ONE-TIME PASSWORD
    61. function anantOTP($length = 8, $chars = 'abcdefghijklmnopqrstuvwxyz1234567890')
    62. {
    63. $chars_length = (strlen($chars) - 1);
    64. $string = $chars{rand(0, $chars_length)};
    65. for ($i = 1; $i < $length; $i = strlen($string))
    66. {
    67. $r = $chars{rand(0, $chars_length)};
    68. if ($r != $string{$i - 1}) $string .= $r;
    69. }
    70. return $string;}
    71. //IF DEBUG VARIABLE IS TRUE, THE RESPONSE OF THE HTTP REQUEST WILL BE WRITTEN TO THE SCREEN
    72. $debug = false;
    73. // IF NOT POSTED ANYTHING YET, THE LOGIN PAGE IS LOADING
    74. if (emptyempty($_POST)){
    75. $i=0;
    76. echo('
    77. <html>
    78. <body>
    79. <h1>One Time Password Form</h1>
    80. <form method="POST">
    81. <table border=1>
    82. <tr>
    83. <td>Username:</td>
    84. <td><input type="text" name="username"></td>
    85. </tr>
    86. <tr>
    87. <td>Password</td>
    88. <td><input type="password" name="password"></textarea></td>
    89. </tr>
    90. <tr>
    91. <td> </td>
    92. <td><input type=submit name=submit value="Get Otp" OnClick="anantSend(this.form);"></td>
    93. </tr>
    94. </table>
    95. </form>
    96. </body>
    97. </html>');}
    98. //IF OTP HAS POSTED YET, anantOTP FUNCTION WILL GENERATE ONE
    99. if (emptyempty($_POST['otphtml'])){
    100. $_SESSION['otp']=anantOTP();
    101. // CHECKING USER CREDENTIALS
    102. if ($password!=$user[$username] || ((emptyempty($_POST['username']) && (!emptyempty($_POST['password'])))) || (emptyempty($_POST['password']) && (!emptyempty($_POST['username']))))
    103. echo ('Please enter a valid username or password!');
    104. elseif ((!emptyempty($_POST['submit'])) && (emptyempty($_POST['password'])) && (emptyempty($_POST['username'])))
    105. echo ('No username or password entered');
    106. elseif($password=$user[$username]){
    107. //SENDING THE PASSWORD AND LOADING THE OTP-VERIFYING PAGE
    108. anantSend($phone[$_POST['username']],'Dear '.$username.'! Your One-Time password is: '.$_SESSION['otp'],$debug);
    109. echo (' <html>
    110. <body>
    111. <h1>Please enter your One-Time password to enter the site!</h1>
    112. <form method="POST">
    113. <table border=1>
    114. <tr>
    115. <td>Your One-time password:</td>
    116. <td><input type="text" name="otphtml"></td>
    117. </tr>
    118. <tr>
    119. <td> </td>
    120. <td><input type=submit name=submit value="Confirm OTP"></td>
    121. </tr>
    122. </table>
    123. </form>
    124. </body>
    125. </html>');
    126. }}
    127. else{
    128. //IF AN OTP HAS ALREADY SENT, CHECKING ITS VALIDITY AND REDIRECTING TO THE PROTECTED CONTENT
    129. $otp1=$_POST['otphtml'];
    130. include('protectedcontent.php');}
    131. ?>
  2. Step 2

    Create another file protectedcontent.php.
    1. <?php
    2. if ($_SESSION['otp']==$otp1){
    3. echo('<html>
    4. <body><h2>You\'ve been successfully verified your One-Time Password</h2></body>
    5. </html>');}
    6. else { echo('<html>
    7. <body><h2>Wrong Password!</h2></body>
    8. </html>');}
    9. ?>