Google recaptcha with my jquery / ajax function

A search for reCaptcha settings seems to lead to the use of jQuery validation. However, I am not sure how to do this in the context of ajax form submission.

I am currently using Validity , not jQuery validation. I have a script link in my header and key location.

Where in this process should I check 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; } }); 
+5
source share
3 answers

Google will verify your retraining for you. An example is below.

 <?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 } 

? >

You can reference docs .

+5
source

You can use the google plugin to check recaptcha in php try the link and then include this code in your check php script

 $recaptcha = new \ReCaptcha\ReCaptcha( RECAPTCHA_SECRET_KEY ); $resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER["REMOTE_ADDR"] ); if(!$resp->isSuccess()) //ERROR CAPTCHA NOT VALIDATED 
+1
source

I have successfully implemented recaptcha on one of my sites. I have not played with him for a long time, so I don’t know exactly how Google deals with this.

But anyway, here is a simplified version of my working code, I hope this can help you.

 <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> 

For a script with src = "https://www.google.com/recaptcha/api.js?hl=en", I noted a link to this link: https://developers.google.com/recaptcha/docs/language .

And server side:

 <?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 } 
-1
source

Source: https://habr.com/ru/post/1244145/


All Articles