Rya*_*ung 3 php validation captcha codeigniter web
新的Google reCAPTCHA使用CodeIgniter 2.2.2每当我尝试运行我的测试时,它会在加载很长时间后返回错误的结果,并显示以下警告消息:
遇到PHP错误
严重性:警告
消息:的file_get_contents(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>
Run Code Online (Sandbox Code Playgroud)
这是我的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>';
}
}
}
?>
Run Code Online (Sandbox Code Playgroud)
(当然我通过谷歌api网页生成了我的公钥和私钥)
伙计们好吗?我是否需要打开我的PHP中的卷曲或什么?
这有可能是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);
Run Code Online (Sandbox Code Playgroud)