Home > OS >  Google recaptcha v3 with codeigniter 3 - incorrect-captcha-sol
Google recaptcha v3 with codeigniter 3 - incorrect-captcha-sol

Time:02-25

I am trying to add recaptchav3 to my site which is built on codeigniter 3 with php8 but i get incorrect-captcha-sol error when I submit the answer. Can any one help me to correct the following ?

// site keys defined in config/constants.php

define('SITE_KEY','XXXXXXX');
define('SECRET_KEY','XXXXXX');

my_view :

<!-- some html header code -->
<?php $site_key=SITE_KEY;?>
<script src="https://www.google.com/recaptcha/api.js?render=<?php echo $site_key; ?>" async defer></script>
</head>

<form name="news_form" id="news_form" action="<?=site_url('account/store_profile'); ?>" method="post" onsubmit="return(validateForm());">
<div  style="display: none;">
<input type="hidden" id="g-recaptcha-response" name="g-recaptcha-response" />
<input type="hidden" id="action" name="action" />
<!--- other elements -->
<button type="submit" name="submit"   id="submit">Send</button>               
<button type="reset" name="reset"   id="cancel">Cancel</button>                
</form>
<?php $site_key= SITE_KEY;?>
<script type="text/javascript">
grecaptcha.ready(function() {
grecaptcha.execute('<?php echo $site_key; ?>', {action: 'profile'})
.then(function(token) {
console.log(token);
document.getElementById('g-recaptcha-response').value=token;
document.getElementById('action').value='profile';
});
});
</script>

controller:

if (isset($_POST['submit'])) {
   $data=$this->input->post();

//form validation code here $this->form_validation->set_rules
$arrResponse = getCaptcha($data['g-recaptcha-response'],$data['action']);  // call to helper function

if($arrResponse["success"] == '1' && $arrResponse["action"] == $action && $arrResponse["score"] >= 0.5) {
    // valid submission
    // go ahead and do necessary stuff
} else {
    // spam submission
    // show error message
}

common_helper.php

function getCaptcha($token,$action){
if(isset($token) && !empty($token)) {
    $secret_key = SECRET_KEY;
    $Response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret_key."&response=".$token."&remoteip=" . $_SERVER['REMOTE_ADDR']);
    $Return = json_decode($Response);
    echo "<pre>";print_r($Response);exit;

error :

{
  "success": false,
  "error-codes": [
    "incorrect-captcha-sol"
  ]
}

CodePudding user response:

This is solved. Here is the solution. Instead of assigning constant keyword to a php variable, i have echo directly the Constant Keyword.

  • Related