谷歌用codeigniter 3重新验证第3版-不正确的验证码

vfhzx4xs  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(135)

我试图添加captchav 3到我的网站,这是建立在代码点火器3与php 8,但我得到了不正确的captcha-溶胶错误,当我提交的答案.可以有谁帮我纠正以下?
//在config/constants.php中定义的站点键

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

我的视图(_V):

<!-- 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"
  ]
}
wko9yo5t

wko9yo5t1#

这个问题解决了。这里是解决方案。我没有将constant关键字赋给php变量,而是直接echo了Constant关键字。

相关问题