Laravel中的Otp验证

时间:2019-12-08 17:45:19

标签: php laravel one-time-password

我正在尝试在laravel应用中获取Otp验证系统 但 当我输入我的号码以获取otp时,它显示页面已过期。(已提供代码和屏幕截图“页面已过期”)

使用MSG91

代码...

 <div class="container">
        <div class="jumbotron">
            <h1>OTP</h1>
            <div class="card">
                <div class="card-body">
                    <div class="col-md-6">

                        <form action="{{url('/')}}/dashboard" method="POST">
                        <div class="form-group">
                            <label for="exampleInputPassword1">Mobile Number</label>
                            <input type="number" class="form-control" id="number" name="number" placeholder="Enter Mobile Number">
                        </div>
                        <button type="submit" name="sendotp" class="btn btn-success">Send OTP</button>

                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <?php


        $authKey = "auth...Key...here";
        $senderId = "Id here";

        if (isset($_POST['sendotp'])){

            $mobileNumber = $_POST['number'];

            $message = 'Your Verification Code id ######';

            $postData = array(
              'authKey' => $authKey,
              'mobiles' => $mobileNumber,
              'message' => $message,
              'sender' => $senderId,
            );

            $url ="http://control.msg91.com/api/sendotp.php";

            $curl = curl_init($url);

            curl_setopt_array($curl, array(
                CURLOPT_URL => "https://api.msg91.com/api/v5/otp?invisible=1&otp=OTP%20to%20send%20and%20verify.%20If%20not%20sent%2C%20OTP%20will%20be%20generated.&userip=IPV4%20User%20IP&authkey=Authentication%20Key&email=Email%20ID&mobile=Mobile%20Number&template_id=Template%20ID&otp_length=&otp_expiry=",
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 30,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "POST",
                CURLOPT_POSTFIELDS => $postData,
                CURLOPT_SSL_VERIFYHOST => 0,
                CURLOPT_SSL_VERIFYPEER => 0,
                CURLOPT_HTTPHEADER => array(
                    "content-type: application/json"
                ),
            ));

            $response = curl_exec($curl);
            $err = curl_error($curl);

            curl_close($curl);

            if ($err) {
                echo "cURL Error #:" . $err;
            } else {
                echo $response;
            }

        }

?>

错误.. enter image description here

请让我知道我的错误在哪里.... 并且让我知道使用MSG91在laravel中添加OTP验证的最佳方法是

谢谢

1 个答案:

答案 0 :(得分:2)

您需要使用表单传递csrf令牌。

<form action="{{url('/')}}/dashboard" method="POST">
     {{ csrf_field() }}
     <div class="form-group">
          <label for="exampleInputPassword1">Mobile Number</label>
          <input type="number" class="form-control" id="number" name="number" placeholder="Enter Mobile Number">
     </div>
     <button type="submit" name="sendotp" class="btn btn-success">Send OTP</button>
</form>
{{ csrf_field() }}  with generate a hidden field with name _token.
<input type="hidden" name="_token" value="token value">

如果您使用的是laravel> 5.6,则也可以使用@csrf

{{csrf_field()}}和@csrf都为您提供了相同的隐藏字段。