簡體   English   中英

重新驗證錯誤; 注意:file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded in

[英]recaptcha error; Notice: file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded in

我正在使用 recaptcha 2 並收到這個奇怪的錯誤:注意:file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded in... 在第 38 行。

代碼是:

<script src='https://www.google.com/recaptcha/api.js'></script>
    </head>

<body>
<?php 
$page="contact";
include("includes/navbar.php"); 
echo '<div id="wrapper">';
  $response = $_POST["g-recaptcha-response"];
    $url = 'https://www.google.com/recaptcha/api/siteverify';
    $data = array(
        'secret' => '6LfJnWQUAAAAANMRQApsnetVjggDqnn4hx7Ltbyz',
        'response' => $_POST["g-recaptcha-response"]
    );
    $options = array(
        'http' => array (
            'method' => 'POST',
            'content' => http_build_query($data)
        )
    );
    $context  = stream_context_create($options);
    $verify = file_get_contents($url, false, $context);
    $captcha_success=json_decode($verify);
    if ($captcha_success->success==false) {
        echo "<h1>You did not prove you are human.</h1><h2> Please go back and complete the form!</h2>";
        exit();
    } 
    else if ($captcha_success->success==true) {
more code here to execute if captcha successfull

觸發錯誤消息的第 38 行是:

$verify = file_get_contents($url, false, $context);

無論是否勾選了機器人框,都會出現錯誤消息。 如果未勾選該框,則會出現“你沒有證明你是人類”消息,如果已勾選機器人框,則代碼將得到正確處理,但仍會出現錯誤消息。

如何刪除錯誤消息? 該站點有一個 SSL 證書,所以我嘗試更改:

$options = array(
            'http' => array (
                'method' => 'POST',
                'content' => http_build_query($data)
            )
        );

到:

$options = array(
            'https' => array (
                'method' => 'POST',
                'content' => http_build_query($data)
            )
        );

並且刪除了錯誤消息,但是即使選中了機器人框,也會出現“你沒有證明你是人”的消息。

我很難過。

問候

托格

我認為您的選項中缺少一個參數,請嘗試如下操作:

$options = array(
    'http' => array (
                'method' => 'POST',
                'content' => http_build_query($data),
                'header' => 'Content-Type: application/x-www-form-urlencoded'
            )
        );

是的,我通過以下方式修復了該問題:

<script src='https://www.google.com/recaptcha/api.js'></script>
    </head>
<body>
<?php 
$page="contact";
include("includes/navbar.php"); 
echo '<div id="wrapper">';
  $response = $_POST["g-recaptcha-response"];
    $url = 'https://www.google.com/recaptcha/api/siteverify';
    $data = array(
        'secret' => '6LfJnWQUAAAAANMRQApsnetVjggDqnn4hx7Ltbyz',
        'response' => $_POST["g-recaptcha-response"]
    );
    $query = http_build_query($data);
    $options = array(
        'http' => array (
            'header' => "Content-Type: application/x-www-form-urlencoded\r\n", 
            "Content-Length: ".strlen($query)."\r\n".
            "User-Agent:MyAgent/1.0\r\n",
            'method' => 'POST',
            'content' => $query
        )
    );
    $context  = stream_context_create($options);
    $verify = file_get_contents($url, false, $context);
    $captcha_success=json_decode($verify);
    if ($captcha_success->success==false) {
        echo "<h1>You did not prove you are human.</h1><h2> Please go back and complete the form!</h2>";
        exit();
    } 
    else if ($captcha_success->success==true) {
more code here to execute if captcha successfull
$secretKey  = '------------Your S-Key---------------';
    $token      = $_POST["g-token"];
    $ip         = $_SERVER['REMOTE_ADDR'];
   
    /* ======================= POST METHOD =====================*/ 
    $url = "https://www.google.com/recaptcha/api/siteverify?";
    $data = array('secret' => $secretKey, 'response' => $token, 'remoteip'=> $ip);
 
    // use key 'http' even if you send the request to https://...
    $options = array('http' => array(
        'method'  => 'POST',
        'content' => http_build_query($data),
        'header' => 'Content-Type: application/x-www-form-urlencoded'
    ));
    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    $response = json_decode($result);
    if($response->success)
    {
        

             echo '<center><h1>Validation Success!</h1></center>';
    }
    else
    {
        echo '<center><h1>Captcha Validation Failed..!</h1></center>';
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM