Php 如何分配';g-recaptcha-response';到一个变量?与';g-recaptcha-response';作为$request内的参数->;验证([]))
所以我想在一个变量中加入“g-recaptcha-response”,以便能够在我的条件下使用它来验证recaptcha,但我还没有做到这一点。有没有办法只使用validate()内数组中的recaptcha字段,因为我的代码会将我重定向回主页。它直接转到else语句 我现在可以使用input()访问请求属性了。让我困惑的是if语句。真正的问题是: 下一个if语句没有获得预期的成功,而是直接转到else,并显示robot消息:Php 如何分配';g-recaptcha-response';到一个变量?与';g-recaptcha-response';作为$request内的参数->;验证([])),php,laravel,recaptcha,laravel-5.5,contact-form,Php,Laravel,Recaptcha,Laravel 5.5,Contact Form,所以我想在一个变量中加入“g-recaptcha-response”,以便能够在我的条件下使用它来验证recaptcha,但我还没有做到这一点。有没有办法只使用validate()内数组中的recaptcha字段,因为我的代码会将我重定向回主页。它直接转到else语句 我现在可以使用input()访问请求属性了。让我困惑的是if语句。真正的问题是: 下一个if语句没有获得预期的成功,而是直接转到else,并显示robot消息: 您可以通过调用$request对象访问请求的所有属性,例如,$req
您可以通过调用
$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',
];
}
}
其思想是,您构建了secret和response主体,并使用该主体向Google请求验证检查,就像您所做的那样,但将查询作为查询参数直接构建到url
PS:您不必返回代码段:)
你不能使用$request->input('g-recaptcha-response')?我试过了,但也不起作用。然后不要使用$request->validate,创建一个新的验证实例,如我在下面的回答中所述。检查:让我看看你刚才给我看的东西我能做什么。但它不接受$request->input('g-recaptcha-response')似乎有点奇怪,您是否尝试过$request->get('g-recaptcha-response')?
$resultados = json_decode($response->getBody()->getContents());
else{
\Session::flash('flash_message','Robot');
return redirect('/');
}
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',
];
}
}