Googlerecaptcha总是返回false

时间:2015-11-19 21:04:13

标签: php recaptcha

我开发了这个类并包含在我的页面内但返回始终为false。看来recaptcha的值$ _POST没有插入进程页面。

你能看一下吗?

感谢

我的班级:

Address:

我的html页面(简化)

  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;
    }
}

我的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 -->

1 个答案:

答案 0 :(得分:0)

在每种情况下都会返回false:

  $response = file_get_contents($url);

  if($response->success===true) {
    return true;
  } else {
    return false;
  }

您需要从Recaptcha API解析Json Response:

    $response = file_get_contents($url);
    $response = json_decode($response , true);
    //reCaptcha success check 
    if($response ['success']){
        return true;
    } else {
        return false;
    }

http://php.net/manual/de/function.json-decode.php