New Google reCAPTCHA Using CodeIgniter 2.2.2 Everytime I try to run my test, it returns a false result after loading for a long time with the following warning message:
A PHP Error was encountered
Severity: Warning
Message: 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): failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
Filename: controllers/cap.php
Here is my html code
<!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>
Here is my php file:
<?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>';
}
}
}
?>
(Of course I generated my public and private key through google api webpage)
Any idea guys? Do I need to turn on curl or something in my php?
It's possible that allow_url_fopen = Off
in your php.ini
, preventing file_get_contents
from opening URLs.
You could just use cURL for this, something like this:
$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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With