简体   繁体   English

Googlerecaptcha总是返回false

[英]Googlerecaptcha always return false

I have developped this class and include inside my page but the return is always false. 我已经开发了此类,并将其包含在我的页面中,但返回始终为false。 It seems the value $_POST of the recaptcha is not inserted process page. 似乎未插入recaptcha的值$ _POST进程页面。

Can you look ? 你能看吗?

Thank 谢谢

my class: 我的课:

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

my html page (simplified) 我的html页面(简体)

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

result of my htmlpage 我的htmlpage的结果

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

my process (simplified) 我的过程(简化)

      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) 

This will return false in every case: 在每种情况下都将返回false:

  $response = file_get_contents($url);

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

You need to parse the Json Response from the Recaptcha API: 您需要从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 http://php.net/manual/de/function.json-decode.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM