php - 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 验证的最佳方法
谢谢你
解决方案
您需要通过表单传递 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
为您提供相同的隐藏字段。
推荐阅读
- gitlab-ci - 无法加载配置“airbnb”以从 - gitlab ci 扩展
- openstack - Devstack 单一接口在 ubuntu 20.04 上不起作用
- functional-programming - 函数式语言类型推断混淆
- git - 不能使用“Git add -i”
- macos - 如何将文件夹从 Multipass 共享到主机?
- c# - Asp.net core Group by 不重复字段
- laravel - 如何使用 Laravel API 从服务器获取产品来实现应用内购买?
- batch-file - 从 CMD 或批处理文件中禁用“时钟更改时通知我”设置
- javascript - 如何在 React 中将 arrayBuffer 转换为 JSON
- flutter - 是否可以在颤动中将html响应转换为json?