Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Hadayat's avatar

Google recaptcha 3 error Secret key is invalid

Sorry for posting here, I know this is not the right place but I've no more option to fix my bug.

I am facing the issue with Google Recaptcha 3 which is implemented in PHP7.

My PHP Code

define('SITE_KEY', '6Lf8c_SiteKey_is_correct_UtIcrAvXE_fW6Ccs6l6');
define('SECRET_KEY', '6Lf8cLQZAA_Secret_is_correct_QICs4B09s-JiyaWGfUJr73Q');

if (isset($_POST['g-recaptcha-response'])) {

    function getRecaptcha($secretkey){
   $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret='".SECRET_KEY."'&response={$secretkey} ");
    $Return = json_decode($response);
    return $Return;

  }

  $response = getRecaptcha($_POST['g-recaptcha-response']);
  var_dump($response);

MY JS CODE:

 grecaptcha.ready(function() {
          grecaptcha.execute('<?php echo SITE_KEY ?>', {action: 'homepage'}).then(function(token) {
              // Add your logic to submit to your backend server here.
              // alert("testing");
              document.getElementById("g-recaptcha-response").value = token;
              console.log(token);
          });
        });

MY HTML FORM

<form method="post">
	<input type="text" name="g-recaptcha-response" id="g-recaptcha-response">
  <input type="submit" name="submit" value="submit">
</form>

I am getting the token but when I submit the request it throws an error that your secret key is invalid. How can I fix this problem.

0 likes
1 reply

Please or to participate in this conversation.