Php 新的Google reCAPTCHA无法与localhost进行验证
新谷歌 使用CodeIgniter 2.2.2 每次我尝试运行测试时,加载很长时间后,它都会返回错误结果,并显示以下警告消息: 遇到一个PHP错误 严重性:警告 信息: 文件获取内容(): 无法打开流:连接尝试失败,因为 关联方在一段时间后未作出适当回应,或 建立的连接失败,因为连接的主机无法连接 回应 文件名:controllers/cap.php 这是我的html代码Php 新的Google reCAPTCHA无法与localhost进行验证,php,codeigniter,validation,web,captcha,Php,Codeigniter,Validation,Web,Captcha,新谷歌 使用CodeIgniter 2.2.2 每次我尝试运行测试时,加载很长时间后,它都会返回错误结果,并显示以下警告消息: 遇到一个PHP错误 严重性:警告 信息: 文件获取内容(): 无法打开流:连接尝试失败,因为 关联方在一段时间后未作出适当回应,或 建立的连接失败,因为连接的主机无法连接 回应 文件名:controllers/cap.php 这是我的html代码 <!DOCTYPE html> <html lang="en"> <head>
<!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>';
}
}
}
?>
您的php.ini
中可能存在allow\u url\u fopen=Off
,阻止文件获取内容打开url
你可以用旋度来做这个,类似这样:
$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);
检查您的php.ini设置,并确保允许
如果我在真正的web托管服务器上,有什么方法可以实现这一点吗?在CentOS上,查看文件/etc/php.ini
。如果服务器运行Ubuntu,请选中/etc/php5/apache2/php.ini
。如果这是一个真正的web服务器(如果是共享主机,则无法更改),则很可能会将其设置为关闭。你真的应该使用cURL吗?你能给我一个使用cURL的例子,并将它与我上面的代码合并吗?让我发布一个例子作为回应,因为我不能在评论中设置它的样式。它说我的CURLOPT_RETURNTRANSFER是未定义的CURLOPT_RETURNTRANSFER
唯一未定义的方法是你使用一个旧版本的PHP而不是4.0.2,这是不太可能的。这是一个PHP通知还是PHP警告?它加载了很长一段时间,因为它看起来好像在服务器的另一端超时。curl响应是:curl Error:--连接在3011毫秒后超时