Googlerecaptcha总是返回false
问题描述:
我已经开发了这个类并包含在我的页面中,但返回总是为false。似乎recaptcha的值$ _POST没有插入进程页面。Googlerecaptcha总是返回false
你能看吗?
感谢
我的课:
namespace Sites\Shop;
use Core\Registry;
use Core\HTML;
class GoogleRecaptcha {
const API = 'https://www.google.com/recaptcha/';
const SECRET_KEY = MODULES_CONTACT_US_GOOGLE_RECAPTCHA_SECRET_KEY;
const SITE_KEY = MODULES_CONTACT_US_GOOGLE_RECAPTCHA_SITE_KEY;
public function script() {
$Template = Registry::get('Template');
$footer ='<!-- contact_us google captcha end -->' . "\n";
$footer .= '<script src="' . self::API . 'api.js" async defer></script>' . "\n";
$footer .='<!-- contact_us google captcha end -->' . "\n";
return Template->addBlock($footer, 'footer_scripts');;
}
public function html($includeNoScript = false) {
$return = '<div class="g-recaptcha" data-sitekey="' . self::SITE_KEY . '"></div>';
if($includeNoScript == true) {
$return .= $this->noScript();
}
). "\n";
return $return;
}
private function noScript() {
$output = '<noscript>';
$output .= '<div style="width: 302px; height: 352px;">';
$output .='<div style="width: 302px; height: 352px; position: relative;">';
$output .= '<div style="width: 302px; height: 352px; position: absolute;">';
$output .= '<iframe src="' . self::API . 'api/fallback?k=' . self::SITE_KEY . '" frameborder="0" scrolling="no" style="width: 302px; height:352px; border-style: none;"></iframe>';
$output .= '</div>';
$output .= '<div style="width: 250px; height: 80px; position: absolute; border-style: none; bottom: 21px; left: 25px; margin: 0px; padding: 0px; right: 25px;">';
$output .= HTML::textAreaField('g-recaptcha-response','', '250px', '80px', 'id="g-recaptcha-response" class="g-recaptcha-response" style="border: 1px solid #c1c1c1; margin: 0px; padding: 0px; resize: none;"');
$output .= '</div>';
$output .= '</div>';
$output .= '</div>';
$output .= '</noscript>';
return $output;
}
public function check($response) {
$url = self::API . 'api/siteverify?secret=' . self::SECRET_KEY . '&response=' . $response . '&remoteip=' . $_SERVER['REMOTE_ADDR'];
$response = file_get_contents($url);
if($response->success===true) {
return true;
} else {
return false;
}
}
public function display() {
$output = '<!-- contact_us secret google captcha start -->'. "\n";
$output .= $this->script();
$output .= $this->html(true);
return $output;
}
}
我的html页面(简体)我htmlpage的
<?php
$GoogleRecaptcha = Registry::get('GoogleRecaptcha');
$contact_us_form .= $form;
$contact_us_form .= $OSCOM_GoogleRecaptcha->display();
$contact_us_form .= HTML::button(IMAGE_BUTTON_CONTINUE, null, null, 'primary', null, null, null, 'submit');
$contact_us_form .= $endform;
?>
结果
<form name="contact" action="http://boutique/index.php?Info&Contact&Process&action=process" method="post" id="contact"><input type="hidden" name="formid" value="a1e5d99fc7361f2c4ed4169a2ff6ae8b" />
<!-- contact_us secret google captcha start -->
<div class="g-recaptcha" data-sitekey=".................."></div>
<button id=submit type="submit" class="btn btn-primary">Continuer</button>
</form>
<!-- contact_us google captcha end -->
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<!-- contact_us google captcha end -->
<!-- contact_us_form end -->
我的过程(简化)
if (isset($_GET['action']) && ($_GET['action'] == 'process') && isset($_POST['formid']) && ($_POST['formid'] == $_SESSION['sessiontoken'])) {
$error = false;
$google_captcha = HTML::sanitize($_POST['g-recaptcha-response']);
var_dump($google_captcha);
var_dump($OSCOM_GoogleRecaptcha->check($google_captcha));
exit;
}
result of var_dump : string(0) "" bool(false)
答
这将在任何情况下返回false:
$response = file_get_contents($url);
if($response->success===true) {
return true;
} else {
return false;
}
您需要解析来自的ReCaptcha API JSON响应:
$response = file_get_contents($url);
$response = json_decode($response , true);
//reCaptcha success check
if($response ['success']){
return true;
} else {
return false;
}
Itry这一点,但没有按没有工作。公共函数getCheck($ google_response){ $ url = self :: API。 'api/siteverify?secret ='。 $ this-> secretKey。 '&响应='。 $ google_response。 '&remoteip ='。 OSCOM :: GetIpAddress(); $ response = file_get_contents($ url); $ response = json_decode($ response,true); if($ response ['success'] == true){ $ this-> errors = array(); 返回true; } else { $ this-> errors = isset($ response ['error-codes'])? $ response ['error-codes']:array(); 返回false; } } – kakashi
$ response Variable的内容是什么? – yannik995