【问题标题】:New Google reCAPTCHA keeps failing to verify with localhost新的 Google reCAPTCHA 一直无法通过 localhost 进行验证
【发布时间】:2015-07-22 18:19:51
【问题描述】:

新的 Google reCAPTCHA 使用 CodeIgniter 2.2.2 每次我尝试运行测试时,它在加载很长时间后都会返回错误结果,并显示以下警告消息:

遇到了 PHP 错误

严重性:警告

消息: 文件获取内容(https://www.google.com/recaptcha/api/siteverify?secret=PUBLICKEY&response=03AHJ_VuvOwCxIKqGoZXeEOWvDxMjYrBsH9HyWpbxg1YmBTThs8gINjC5yEQOG0fVikDPY3GXemQVrB6DT965o0LARL2rRDP-U6m9Y9DSFdDvz55vwsewe4--m0NfssykJZ3et6zItKH7mNsDIi1LLtPrn7vaQmCGlK3LW2hS4Q8TMDhjTW-tecmCRbonCcTcvMN4gHnWuGzSzUFUlOPxAlSHEvkBHspZb5SnGLNakZ5rrF591LL9SS8NrZErFVO3EySpW_CCG26uDhpMJW5y5B_3nZnBYZqpmf0eIIMy5w3rk2FjJrPw2G8Kj98Cv1B1xJcXBgML7uCnZRmc7WDZhzFoI2JAeuEBNwQkQJvSGXsGLGkewz1ClPiQwzYZRlwEpgo2Zpkri6lrIlNMIc0dtmhM7U172LGELgjbNKFAW29Aq8wTOCwC2tztWlTn_8mq9SrS_mhhs7iaG&remoteip=127.0.0.1): 无法打开流:连接尝试失败,因为 关联方在一段时间后没有正确响应,或 建立连接失败,因为连接的主机未能 回应。

文件名:controllers/cap.php

这是我的html代码

  <!DOCTYPE html>
<html lang="en">
  <head>
    <title>TEST</title>
  </head>

  <body>

    <form action="validation" method="post">

      <label for="name">Name:</label>
      <input name="name" required><br />

      <label for="email">Email:</label>
      <input name="email" type="email" required><br />

      <div class="g-recaptcha" data-sitekey="My_Public_Key"></div>

      <input type="submit" value="Submit" />

    </form>

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

  </body>
</html>

这是我的 php 文件:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class cap extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
        $this->load->database();
        $this->load->model('users_model', 'user');
        $this->lang->load("message",$this->session->userdata('language'));
    }
  public function index() {

    $this->load->view("cap");
  }
    public function validation()
    {
        $email;$comment;$captcha;
        if(isset($_POST['email'])){
          $email=$_POST['email'];
        }if(isset($_POST['comment'])){
          $email=$_POST['comment'];
        }if(isset($_POST['g-recaptcha-response'])){
          $captcha=$_POST['g-recaptcha-response'];
        }
        if(!$captcha){
          echo '<h2>Please check the the captcha form.</h2>';
          exit;
        }
        remoteip=".$_SERVER['REMOTE_ADDR']);
        $response=json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=MyPrivateKey&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true);

        if($response['success'] == false)
        {
          echo '<h2>Wrong</h2>';
        }else
        {
          echo '<h2>Correct</h2>';
        }
    }
}
?>

(当然我的公钥和私钥是通过google api网页生成的)

有什么想法吗?我需要在我的 php 中打开 curl 或其他东西吗?

【问题讨论】:

  • 检查您的 php.ini 设置并确保 allow_url_fopen = On 存在于某处
  • 如果我在真正的虚拟主机服务器上,我将如何实现这一目标?
  • 在 CentOS 上,查看文件 /etc/php.ini。如果服务器运行 Ubuntu,请检查 /etc/php5/apache2/php.ini。如果这是一个真正的网络服务器(如果它是共享主机,你不能改变它),很可能这会变成Off。你真的应该为此使用 cURL
  • 您能否提供一个使用 cURL 的示例并将其与我上面的代码合并?
  • 让我发布一个示例作为回复,因为我无法在评论中设置样式

标签: php codeigniter validation web captcha


【解决方案1】:

allow_url_fopen = Off 可能在您的php.ini 中,阻止了file_get_contents 打开 URL。

您可以为此使用 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);

【讨论】:

  • 它说我的 CURLOPT_RETURNTRANSFER 未定义
  • CURLOPT_RETURNTRANSFER 未定义的唯一方法是您使用早于 4.0.2 的 PHP 版本,这真的不太可能。这是 PHP 通知还是 PHP 警告?
  • 它正在加载很长时间,因为它看起来在服务器的另一端超时。
  • curl 响应为:Curl 错误:--连接在 3011 毫秒后超时
猜你喜欢
  • 2019-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-01
  • 2014-01-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多