gpt4 book ai didi

发送OTP的php实现

转载 作者:搜寻专家 更新时间:2023-10-31 21:52:16 25 4
gpt4 key购买 nike

我正在尝试为我的网站的登录验证实现 sendOTP。我正在使用 https://github.com/rwalkover/sendOTPSample-PHP 提供的代码

这有两个部分; 1. 生成 OTP 和 2. 验证 OTP在原始代码中,两者都是通过调用 php 函数的 ajax 完成的。

我已经能够成功集成第一部分,其中生成的 OTP 被传送到手机。我希望通过将表单提交到 php 来完成验证部分。

请查看位于https://github.com/rwalkover/sendOTPSample-PHP/blob/master/sendotp.php 的sendotp.php 文件

我已经尝试实现以下内容:html 表单

<form class="form-horizontal" id="verifyOtpForm" style="display:none" method = "post" action = "verifyotp.php">
<div class="form-group">
<label class="control-label col-sm-2" for="email">Enter code:</label>
<div class="col-sm-8">
<input type="text" class="form-control" name="oneTimePassword" placeholder="Enter OTP received by SMS" id="oneTimePassword">
</div>
<div class="col-sm-2">
<input type="submit" class="btn btn-primary btn-md btn-block" name="verifyOtp" id="verifyOtp" value="Verify OTP" >
</div>
<input type="hidden" name="hiddenCode" id="hiddenCode">
<input type="hidden" name="hiddenNumber" id="hiddenNumber">
</form>

verifyotp.php:

session_start();
$baseUrl = "https://sendotp.msg91.com/api";
if(isset($_POST['oneTimePassword'])){
if ($_POST['oneTimePassword'] == $_SESSION["oneTimePassword"]) {

$data = array("countryCode" => $_POST['hiddenCode'], "mobileNumber" => $_POST['hiddenNumber'], "oneTimePassword" => $_POST['oneTimePassword']);
$data_string = json_encode($data);
$ch = curl_init($baseUrl . '/verifyOTP');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
'application-Key: my key goes here'
));
$result = curl_exec($ch);
curl_close($ch);
$response = json_decode($result, true);
if ($response["status"] == "error") {
//echo $response["response"]["code"];
header("location: index.php");
} else {
header("location: ../index.php");
}
}
}

我相信有更好的方法来做到这一点。我对 curl 及其作用一无所知。任何帮助将不胜感激。

最佳答案

您不需要验证 OTP 两次。

第一个选项是从 session 验证 OTP,第二个选项是调用 sendOTP 验证 api。

如果您想通过 sendOTP 验证,请使用此代码:

    $baseUrl = "https://sendotp.msg91.com/api";
if(isset($_POST['oneTimePassword'])){
$data = array("countryCode" => $_POST['hiddenCode'], "mobileNumber" => $_POST['hiddenNumber'], "oneTimePassword" => $_POST['oneTimePassword']);
$data_string = json_encode($data);
$ch = curl_init($baseUrl . '/verifyOTP');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
'application-Key: my key goes here'
));
$result = curl_exec($ch);
curl_close($ch);
$response = json_decode($result, true);
if ($response["status"] == "error") {
header("location: index.php");
} else {
header("location: ../index.php");
}
}

或者,如果您想在最后验证,请使用此代码:

if(isset($_POST['oneTimePassword'])){
if ($_POST['oneTimePassword'] == $_SESSION["oneTimePassword"]) {
header("location: index.php");
} else {
header("location: ../index.php");
}
}

关于发送OTP的php实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39397996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com