php - 如何将“g-recaptcha-response”分配给变量?'g-recaptcha-response' 是 $request->validate([]) 中的一个参数
问题描述
所以我想要的是将'g-recaptcha-response'放在一个变量中,以便能够在我的条件下使用它来验证recaptcha,但我无法做到这一点。有没有办法只使用 validate() 中的数组中的 recaptcha 字段,因为我的代码原样将我重定向回主页。它直接进入 else 语句。
public function contactanospost(Request $request){
$request->validate([
'nombre' => 'required|distinct',
'telefono'=> 'required|telefono',
'correo' => 'required|email',
'mensaje' => 'required',
'g-recaptcha-response' => 'required|captcha',
]);
if(POST['g-recaptcha-response']){ /*here I am trying to
check with a condition if recaptch was really validated once the form is submitted */
$token= POST['g-recaptcha-response'];
$client = new Client();
$response = $client->post('https://www.google.com/recaptcha/api/siteverify', [
'form_params' => array('secret' => 'mycaptchakey',
'response'=> $token /*Checking with google parameters for recaptcha if the user was indeed verified */
)
]);
$resultados = json_decode($response->getBody()->getContents()); //decode with json
if($resultados->success){ /*if this was a success then return a page with parameters from google recaptcha such as: secret, response, remote ip.*/
dd($resultados); /*show the results from verified user and that recaptcha is working*/
$contactanos = Contactanos::create($request->all());/* and create all fields for model Contactanos*/
Mail::to('some@mail')->send(new enviacorreo($contactanos)); /* since it is a contact form then send all the information to some mail*/
\Session::flash('flash_message','Tu mensaje ha sido enviado!'); /* send a message with "email delivered" verification*/
return redirect()->back();
}
else{
\Session::flash('flash_message','Robot');
return redirect('/');
}
}
else{
return redirect('/');
}
}
我现在可以使用 input() 访问请求属性,让我感到困惑的是我的 if 语句。真正的问题在于:
$resultados = json_decode($response->getBody()->getContents());
下一个 if 语句没有获得预期的成功,而是直接进入 else 并带有机器人消息:
else{
\Session::flash('flash_message','Robot');
return redirect('/');
}
解决方案
您可以$request
通过调用从对象访问请求的所有属性,例如,如果您已阅读文档,$request->input('g-recaptcha-response')
这是访问请求的基础。
我可以借给您一个片段来执行此操作,也许它会帮助您重新考虑如何验证验证码:
use GuzzleHttp\Client;
....
$v = Validator::make($request->all(), [
'name' => 'required|min:2',
'email' => 'required|email|max:255',
'subject' => 'sometimes|required|min:3',
'message' => 'required|min:3',
'g-recaptcha-response' => 'sometimes|required'
], [
'g-recaptcha-response.*' => 'Please verify that you are not a robot'
]);
if ($v->fails()) {
return [
'success' => 'no',
'data' => $v->errors()->first()
];
}
if ($request->get('g-recaptcha-response')) {
$verify_form = [
'secret' => env('GOOGLE_RECAPTCHA_SECRET', 'default'), //better to save in config though
'response' => $request->get('g-recaptcha-response')
];
$client = new Client();
$verify_serial = '?'.http_build_query($verify_form);
$response = $client->post('https://www.google.com/recaptcha/api/siteverify'.$verify_serial);
$arrayed_response = json_decode($response->getBody()->getContents(), true);
if(!$arrayed_response['success']){
Log::notice('There is something wrong with the verification of recaptcha: ',$arrayed_response );
return [
'success' => 'no',
'data' => 'Something went wrong in verification process',
];
}
}
这个想法是,您构建秘密和响应主体,并使用它向 Google 请求验证检查,就像您所做的一样,但将查询构建为直接到 url 的查询参数。
PS:您不必返回片段:)
推荐阅读
- css - 做第一个
- 在单独的一排,然后休息
- 在两行和列中
- csv - 在文件上按顺序使用不同的阅读器
- xamarin - 替换 MvvmCross 中的默认 IoC 容器
- python - Python - 覆盖已在输出中打印的文本
- java - 防止运行实现 CommandLineRunner 的 spring boot 任务
- php - Yii2 GridView:SQLSTATE [23000]:违反完整性约束:1052 列 'id' 在 order 子句中不明确
- performance - Expo build:web 构建巨大的捆绑包和缓慢的网站,性能不佳
- lisp - 如何在clisp中创建和写入文件而不覆盖(只是添加)
- javascript - 如何通过为 javascript/nodejs 中的节点元素分配值来格式化 JSON
- image - 如何使用 Flutter 中的 Provider 包更改图像