Google Recaptcha V3 Code implement Guide.
Copy Below Code
View As A Text File
Show Text Only
Show API
Edit Code
/*View JS Cocde*/
<script src="https://www.google.com/recaptcha/api.js?render=6Lf-PzcaAAAAADvlxlELzapq-pDUvAyDHtDmmA6l"></script>
///////////////////Footer JS
@section('beforeBodyClose')
<script type="text/javascript">
$('#frm').submit(function(event) {
event.preventDefault();
grecaptcha.ready(function() {
grecaptcha.execute('6Lf-PzcaAAAAADvlxlELzapq-pDUvAyDHtDmmA6l', {action: 'form_contact'}).then(function(token) {
$('#frm').prepend('<input type="hidden" name="token" value="' + token + '">');
$('#frm').prepend('<input type="hidden" name="action" value="form_contact">');
// $('#frm').unbind('submit').submit();
//submitMyFormContact(frm,'https://www.madmarecrossfit.com/post_contact', false, 'Form has been submitted Successfully.');
quick_submit('{{base_url()}}schedule-market-research-consultation.html',frm)
return false;
});;
});
});
</script>
@endsection
/*Controller Code*/
if(recaptcha_v3_validate()==false)
{
echo json_encode(array(
'success' => 'error',
'errormsg' => 'recaptcha is required',
'key' => 'recaptcha',
'val' => ''
));
exit;
}
//////////////////////////helper function///////////////////////////////////////////////
function recaptcha_v3_validate()
{
$RECAPTCHA_V3_SECRET_KEY = '6Lf-PzcaAAAAAJfBJ7jIBfh41hXURx57RoyHc8ob';
$token = $_POST['token'];
$action = $_POST['action'];
// call curl to POST request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('secret' => $RECAPTCHA_V3_SECRET_KEY, 'response' => $token)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$arrResponse = json_decode($response, true);
// verify the response
if ($arrResponse["success"] == '1' && $arrResponse["action"] == $action && $arrResponse["score"] >= 0.5) {
return true;
} else {
return false;
}
}