如何生成OTP并通过短信将密码发送到移动设备

问题描述 投票:4回答:5

我正在做一个使用OTP登录网站的项目,我创建了一个名为“Generate”的按钮,一旦点击它将创建OTP并通过HTTP网关发送短信,然后将密码存储在数据库中。

我创建OTP并保存在DB中的代码:

if(isset($_POST['generate']))
{
    $string = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $string_shuffled = str_shuffle($string);
    $password = substr($string_shuffled, 1, 7);

    $password = base64_encode($password);
    $query = mysql_query("UPDATE user_login SET password='".$password."' WHERE username = 'ajai sandy' ");
    $qry_run = mysql_query($query);
}

现在我需要放置这个SMS API代码:

http://login.smsgatewayhub.com/smsapi/pushsms.aspx?user=abc&pwd=xyz&to=919898123 456&sid=senderid&msg=test%20message&fl=0 

事情是第5行代码生成OTP,然后我需要在此之后放置我的SMS API,以便它可以将密码发送到移动设备,然后它应该加密第6行的密码,然后保存在数据库。

我不知道如何按顺序执行此操作,也不知道将代码放在何处

php sql api login one-time-password
5个回答
5
投票

试试这个。

if(isset($_POST['generate']))
{
    $string = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $string_shuffled = str_shuffle($string);
    $password = substr($string_shuffled, 1, 7);

    file_get_contents("http://login.smsgatewayhub.com/smsapi/pushsms.aspx?user=abc&pwd=$password&to=919898123456&sid=senderid&msg=test%20message&fl=0");


    $password = base64_encode($password);
    $query = mysql_query("UPDATE user_login SET password='".$password."' WHERE username = 'ajai sandy' ");
    $qry_run = mysql_query($query);
}

1
投票

以下代码就像魅力,

 header('Location:http://login.smsgatewayhub.com/smsapi/pushsms.aspx?user=abc&pwd=$password&to=919898123456&sid=senderid&msg=test%20message&fl=0');

1
投票

谢谢,我很高兴为您推荐这个很棒的教程

//OTP SYSTEM CODE

function sendSMS($mobile=null, $subject=null)
{
$SMSapiKey = 'XYZ';
$url = 'http://example.com/api_2.0/SendSMS.php?APIKEY='.$SMSapiKey.'&MobileNo='.urlencode($mobile).'&SenderID=SAMPLE_MSG&Message='.urlencode($subject).'&ServiceName=TEMPLATE_BASED';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$returndata = curl_exec($ch);
curl_close($ch);
return "A SMS SENT SUCCESSFULLY TO $mobile";
}
$otp_code = strtoupper(substr(md5(uniqid()), 0, 6));   // A smart code to generate OTP PIN.

查看这个令人敬畏的教程和G2FA的实现,用于加密图形安全的OTP make otp system using php


0
投票

这是一个使用http://2Factor.in OTP API通过PHP发送OTP的快速示例代码

 <?php

$YourAPIKey='<YourAPI>';
$SentTo='<User10DigitNumber>';


### DO NOT Change anything below this line
$agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
$url = "https://2factor.in/API/V1/$YourAPIKey/SMS/$SentTo/AUTOGEN"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
echo curl_exec($ch); 
curl_close($ch);

您可以参考THIS LINK,说明从PHP实现SMS OTP的快速步骤


0
投票

Verifying a phone number using OTP(一次性密码)是减少网站垃圾邮件的必然方法。在本文中,我们将非常详细地讨论这个主题。我们将学习如何设置PHP代码以将OTP发送到移动号码,从而验证用户。我们将使用Twilio作为第三方服务来发送包含一次性密码(OTP)的消息。

实际上,Twilio是第三方工具,可用于发送短信,IVR等等。 Twilio有很多替代品,但由于它的无缝集成和我个人的选择,我们将在文章中使用Twilio。尽管Twilio是一种付费工具,但我们可以使用它的“免费版本”,当然它有一定的局限性,但它足以满足我们的文章和我们的目的。

© www.soinside.com 2019 - 2024. All rights reserved.