首页 > 解决方案 > laravel 中的 OTP 验证

问题描述

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

使用味精91

代码...

 <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;
            }

        }

?>

错误.. 在此处输入图像描述

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

谢谢你

标签: phplaravelone-time-password

解决方案


您需要通过表单传递 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() }} will generate a hidden field with name _token.
<input type="hidden" name="_token" value="token value">

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

两者{{ csrf_field() }}@csrf为您提供相同的隐藏字段。


推荐阅读