Google recaptcha v3 with codeigniter 3 - incorrect-captcha-sol

Google recaptcha v3 with codeigniter 3 - incorrect-captcha-sol

我正在尝试将 recaptchav3 添加到我的网站,该网站是使用 php8 基于 codeigniter 3 构建的,但是当我提交答案时出现错误的验证码溶胶错误。任何人都可以帮我更正以下内容吗?

// 在 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 class="input-cover f5" 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"  class="button" id="submit">Send</button>               
<button type="reset" name="reset"  class="button cancel" 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>

控制器:

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;

错误:

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

已解决。这是解决方案。 我没有将常量关键字分配给 php 变量,而是直接回显了常量关键字。