检查reCaptcha服务器端时发出警告

时间:2016-05-30 06:28:49

标签: php recaptcha

当我检查服务器端的reCAPTCHA时,我收到警告。 这是我的错误:

  

警告:file_get_contents(https://www.google.com/recaptcha/api/siteverify?secret=mysecretkey&response=03AHJ_VuvejqLNxxSFNgqAH5ma83UyRQkowutkQBVlxMrsvTH_IyVhOyLWNYgoOZQGEDu0Dt6wMo2xCkcsFv44tcRAh1ysPdJvqSraNxD9e422YKjdxV0ngz1_8kz0QarboIMxKwCjcuoQnPLZGRFf9w9it_JrT9BXFh3AXFb3NZgH3bQGJUz_46TgGATy4-UdKLHyWZG3_CXoIsJyp8lJmzqd4vQoHrqJ2WZSQTZpQsKoww7D399xQqwbd2I5KjGkQblfvj3dAKRGz-vCQ_TRlGmU4PmSu9azdWSCocDu2suYdWNaazunvvYV0dPtG7TGS9YmxbZuz5b0YdSTJqHSwhLdF3i8B5apS_KI1F3K45rrEO88-HlNfxUYzAAkm6mxLtSjfzp_73UUtn2o7k0dQUpFaCnwQuv47a41fBNetIERmsXl7nmFGnRb_SOzbKcYaBueNlhvBvYa8p3lRtN1wA2Fuact9kvDB78fDAIZ2oPaeIDj-_Qme17PPFmQKsyh6Yd--pDu2rz0oSIEU5lWrXslVsdLwV89az4ImndilWbRvNFKMcYoKDuTiaXXWTPu50H5m9bLuCAIw-A0JuDZt6aowm4rgh3HxNTlkD5xBi3An-T7A-oPzxk4AStRBEfNTkEiUbvLiigXIdzvoJCNqJHq9ZWKgJ6k5I0Gm_O5dblIb7QAuqB-zBPV5lL-hrESF9bzoXFdn-P8-AVL9Pg1vr8V0PfPOyjLg3-A5dSvi9HT7GIBbkvnu-FXdNCO7RXy-aQqqHRmgs7Tl6-6lStdgSvwr1kIWxrXo9Wry6AzWZ1SdYUPjHMYhxhTmtGVImwHOELTVHwjnXchrRPJym8_8aY2BXfPSlZWBnT8WodHn-liEAP1yDEWKsTjotiUfwM

我理解"无效输入秘密"因为我使用过" mysecretkey"在上面的网址中。问题是"无效输入 - 响应"。 我在下面粘贴我的代码。

$secretKey = "mysecretkey";
$ip = $_SERVER['REMOTE_ADDR'];
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$_POST["g-recaptcha-response"]."&remoteip=".$ip);
$responseKeys = json_decode($response,true);
if(intval($responseKeys["success"]) !== 1)
{
    echo "you're a robot";
} else {
    echo "form's control";
}

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

也许allow_url_fopen = Off中的php.ini阻止了file_get_contents打开网址。

您可以使用cURL,如下所示:

 $fields = array(
        'secret'    =>  "MySecretKey",
        'response'  =>  $captcha,
        'remoteip'  =>  $_SERVER['REMOTE_ADDR']
    );
    $ch = curl_init("https://www.google.com/recaptcha/api/siteverify");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
    $response = json_decode(curl_exec($ch));
    curl_close($ch);

答案 1 :(得分:-1)

添加

$secretKey = "mysecretkey";
$captcha = $_POST["g-recaptcha-response"]; //Add This Line

尝试:

$url="https://www.google.com/recaptcha/api/siteverify?secret={$secretkey}&response={$captcha}&remoteip={$ip}";

$options = array(
    'secret'    =>  "MySecretKey",
    'response'  =>  $captcha,
    'remoteip'  =>  $_SERVER['REMOTE_ADDR']
);
$context = stream_context_create( $options );
$responseKeys=json_decode( file_get_contents( $url, FILE_TEXT, $context ) );
if( $responseKeys->success ){
/* everything is ok */
}
else{ 
/* captcha failed */ 
}