发送OTP的php实现

发送OTP的php实现,php,ajax,curl,Php,Ajax,Curl,我正在尝试为我的网站的登录验证实现sendOTP。我使用的代码可以在 这有两个部分;1.生成OTP和2。核实检察官办公室 在原始代码中,两者都是通过调用php函数的ajax完成的 我已经成功地集成了第一部分,其中生成的OTP被发送到移动电话。我希望通过将表单提交给php来完成验证部分 请查看位于的sendotp.php文件 我已尝试实施以下措施: html表单 <form class="form-horizontal" id="verifyOtpForm" style="display:n

我正在尝试为我的网站的登录验证实现sendOTP。我使用的代码可以在

这有两个部分;1.生成OTP和2。核实检察官办公室 在原始代码中,两者都是通过调用php函数的ajax完成的

我已经成功地集成了第一部分,其中生成的OTP被发送到移动电话。我希望通过将表单提交给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两次

第一个选项是从会话验证OTP,第二个选项是调用sendotpverifyAPI

如果要通过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");
        }
    }
if(isset($_POST['oneTimePassword'])){
    if ($_POST['oneTimePassword'] == $_SESSION["oneTimePassword"]) {
            header("location: index.php");
        } else {
            header("location: ../index.php");
        }
    }