php 如何生成OTP并将密码通过短信发送到手机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22352471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:51:36  来源:igfitidea点击:

how to generate OTP and send the password to mobile via sms

phpsqlapiloginone-time-password

提问by Ajai Sandy

I am doing a project of using OTP for the login of the websites, I have created a button named "Generate" once clicked it will create an OTP and send an SMS via HTTP gateway, then it stores the password in the database.

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

My code to create an OTP and save in DB:

我创建 OTP 并保存在数据库中的代码:

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);
}

Now I need to place this code of SMS API:

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

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

The thing is the 5th line of code generates the OTP, then I need to place my SMS API after this so that it could send the password to the mobile, then it should encrypt the password which is on the 6th line and then saves in the database.

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

Am not sure how to perform this action in sequence and don't know where to place the code

不确定如何按顺序执行此操作,也不知道将代码放在哪里

采纳答案by Nishant Solanki

Try this.

尝试这个。

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);
}

回答by Ajai Sandy

The following code works like a charm ,

下面的代码就像一个魅力,

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

回答by RASAtech India

Thanks, I am happy to refer you this awesome tutorial

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

//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.

Checkout this awesome tutorial and implementation of G2FA for crypto-graphically secured OTP make otp system using php

查看这个很棒的教程和 G2FA 的实现,用于加密图形安全的 OTP 使用 php 制作 otp 系统

回答by Rahul

Here is a quick sample code to send OTP via PHP with http://2Factor.inOTP API

这是一个快速示例代码,用于使用http://2Factor.inOTP 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);

You may refer to THIS LINKillustrating quick steps to implement SMS OTP from PHP

您可以参考此链接说明从 PHP 实现 SMS OTP 的快速步骤

回答by Ayush Jain

Verifying a phone number using OTP(One Time Password) is a sure shot way to reduce the spam over your website. In this article, we will be going to discuss this topic in great detail. We will be learning how we can set up our PHP code to send the OTP to the mobile numbers and hence verifying the user. We would be using Twilio as a third party service to send the messages containing One Time Passwords (OTPs).

使用 OTP(一次性密码)验证电话号码是减少网站垃圾邮件的可靠方法。在本文中,我们将详细讨论这个话题。我们将学习如何设置 PHP 代码以将 OTP 发送到手机号码,从而验证用户。我们将使用 Twilio 作为第三方服务来发送包含一次性密码 (OTP) 的消息。

Actually, Twilio is a third party tool which can be used to send text messages, IVR and much more. There are many alternatives to Twilio but due to its seamless integration and a bit of my personal choice, we would be using Twilio here in our article. Although Twilio is a paid tool, we can use a "free version" of it which of course has certain limitations but it would suffice for our article and hence our purpose.

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