【问题标题】:php implementation of send OTP发送 OTP 的 php 实现
【发布时间】: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 及其作用一无所知。任何帮助将不胜感激。

【问题讨论】:

    标签: php ajax curl


    【解决方案1】:

    您无需验证 OTP 两次。

    第一个选项是从会话验证 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");
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2020-02-16
      • 2020-01-08
      • 1970-01-01
      • 2021-02-15
      • 2016-08-02
      • 1970-01-01
      • 1970-01-01
      • 2021-08-25
      • 1970-01-01
      相关资源
      最近更新 更多