La búsqueda sobre la configuración de reCaptcha parece conducir al uso de la validación de jQuery. Sin embargo, no estoy seguro de cómo hacer esto en el contexto de un envío de formulario ajax.
Actualmente estoy usando Validity , no jQuery Validation. Tengo la referencia del script en mi encabezado y la clave en su lugar.
¿En qué parte del proceso debo validar el reCaptcha?
<form id="intake-form" class="grid-form" action="javascript:void(0);"> ... <div class="g-recaptcha" data-sitekey="key_here"></div> <input type="submit" name="submit" value="Send!" /> <br /> <p id="formstatus"></p> </form> $("#intake-form").submit(function () { var str = $(this).serialize(); if (validateIntakeForm()) { // validate intake fields with validity $.ajax({ type: "POST", url: global['base']+"intakeform", data: str, success: function (msg) { $("#formstatus").ajaxComplete(function (event, request, settings) { if (msg == 'success') { result = '<div class="successmsg">Your request has been sent.'; $('#intake-form').clearForm(); } else { result = 'There was a problem sending your message.<br />' + msg; } $(this).html(result); }); } }); return false; } });Google validará tu re-captacha por ti. Consulte el siguiente ejemplo.
<?php //first i retrieved the recaptcha coming through post method. //i have used post method, you can use any method you want. $recaptcha = $_POST['g-recaptcha-response']; //After retrieving send a post curl request to verify it. //you will have to send your secret key along with it. $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,"secret=*******************************=".$recaptcha); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $server_output = curl_exec ($ch); $server_output = json_decode($server_output,true); curl_close ($ch); if($server_output['success']){ // captacha validated successfully. }else{ // invalid captcha }?>
Puede consultar los documentos .
Puede usar el complemento de Google para validar recaptcha en php, pruebe este enlace y luego incluya este código en su secuencia de comandos de validación de php
$recaptcha = new \ReCaptcha\ReCaptcha( RECAPTCHA_SECRET_KEY ); $resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER["REMOTE_ADDR"] ); if(!$resp->isSuccess()) //ERROR CAPTCHA NOT VALIDATEDImplementé con éxito recaptcha en uno de mis sitios web. No he jugado con él desde hace mucho tiempo, así que no sé exactamente cómo Google maneja esto.
Pero de todos modos, aquí hay una versión simplificada de mi código de trabajo, espero que pueda ayudarlo.
<body> <form method="post" action=""> <!-- your regular form fields here... --> <div class="g-recaptcha" data-callback="processCaptcha" data-sitekey="THE_KEY_HERE"></div> <!-- or your regular form fields here... --> <input type="submit" value="Send"/> </form> <script type="text/javascript" src="https://www.google.com/recaptcha/api.js?hl=eng"></script> <script src="https://www.google.com/recaptcha/api.js" async defer></script> </body>Para el script con src="https://www.google.com/recaptcha/api.js?hl=eng", anoté una referencia a este enlace: https://developers.google.com/recaptcha/docs/ lenguaje
Y del lado del servidor:
<?php $secret = "THE_KEY_HERE"; $errors = []; $gRecaptchaResponse = $_POST["g-recaptcha-response"]; $recaptcha = new \ReCaptcha\ReCaptcha($secret); // this class is provided by google $resp = $recaptcha->verify($gRecaptchaResponse, $_SERVER['REMOTE_ADDR']); if (false === $resp->isSuccess()) { // $errors = $resp->getErrorCodes(); $errors[] = "Boo, you are a bot"; } if (empty($errors)) { // your success routine here } else { // your error routine here }