【发布时间】:2017-01-16 19:16:27
【问题描述】:
我正在尝试为我的网站的登录验证实施 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>
验证otp.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 及其作用一无所知。任何帮助将不胜感激。
【问题讨论】: