2015-11-19 140 views
0

我已經開發了這個類幷包含在我的頁面中,但返回總是爲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) 

回答

0

這將在任何情況下返回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; 
    } 

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

+0

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

+0

$ response Variable的內容是什麼? – yannik995