vendredi 29 mai 2020

How to verify hCaptcha response and ensure it is filled?

I have followed the tutorial here.

hCaptcha appears on my site, but the form can be submitted without attempting it. How can I make it a required field?

I have tried the following to make it required.

<?php
$data = array(
            'secret' => "my-secret (should start with 0x..)",
            'response' => $_POST['h-captcha-response']
        );
$verify = curl_init();
curl_setopt($verify, CURLOPT_URL, "https://hcaptcha.com/siteverify");
curl_setopt($verify, CURLOPT_POST, true);
curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($verify, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($verify);
var_dump($response);
?>

And also

<?php

  if(isset($_POST['h-captcha-response']) && !empty($_POST['h-captcha-response']))
  {
        $secret = 'your_secret_key';
        $verifyResponse = file_get_contents('https://hcaptcha.com/siteverify?secret='.$secret.'&response='.$_POST['h-captcha-response'].'&remoteip='.$_SERVER['REMOTE_ADDR']);
        $responseData = json_decode($verifyResponse);
        if($responseData->success)
        {
            $succMsg = 'Your request have submitted successfully.';
        }
        else
        {
            $errMsg = 'Robot verification failed, please try again.';
        }
   }
?>

But I get a Notice: Undefined Index: error. I tried to hide the error by editing php.ini and also by adding
<?php error_reporting (E_ALL ^ E_NOTICE); ?> at the top of my file, but it doesn't work.

When I try to make it required, I get an unexpected end to file error-

$("form").submit(function(event) {

   var hcaptchaVal = $('[name=h-captcha-response]').value;
   if (hcaptchaVal === "") {
      event.preventDefault();
      alert("Please complete the hCaptcha");
   }
});



Aucun commentaire:

Enregistrer un commentaire