验证Recaptcha Ajax和Codeigniter [英] Validate recaptcha ajax and codeigniter
本文介绍了验证Recaptcha Ajax和Codeigniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Codeigniter和Ajax验证并提交电子邮件表单。
我尝试了许多方法,它一直给我结果{ sent:false},在添加Recaptcha行之前,它的效果很好。
I am using Codeigniter and Ajax to validate and submit the email form. I tried many methods, it keeps giving me the result {"sent":false} it was working great before I add the recaptcha lines.
这是我的代码:
custom.script.js
$("#ajax-contact-form").submit(function() {
var href = 'http://localhost:8888/en/send';
var name = $("#form-name").val();
var email = $("#email").val();
var phone = $("#phone").val();
var title = $("#form-title").val();
var message = $("#form-message").val();
$.ajax({
type: "POST",
url: href ,
dataType: "json",
data: {
name: name,
email: email,
phone: phone,
title: title,
message: message,
//UPDATE from recaptcha without quotes to 'g-recaptcha-response'
'g-recaptcha-response': grecaptcha.getResponse()
},
success: function(msg) {
if(msg.sent) {
console.log("Successssssssssss");
$('#result').addClass('success');
$('#result').html('Email was sent successfully!');
} else {
$('#result').addClass('error');
$('#result').html('Email was NOT sent!');
}
}
});
return false;
});
En.php
function send()
{
$this->load->helper('form');
$this->load->library('form_validation');
$google_url="https://www.google.com/recaptcha/api/siteverify";
//UPDATE from recaptcha to 'g-recaptcha-response'
$str = $this->input->post('g-recaptcha-response');
$secret = 'GOOGLE_SECRET_KEY';
$url=$google_url."?secret=".$secret."&response=".$str;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$status= json_decode($output, true);
if ($status['success']) {
$this->load->library('email');
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['wordwrap'] = TRUE;
$this->email->initialize($config);
$body = 'ANY TEXT';
$emailto = "email@domain.com";
$this->email->from($this->input->post("email"), $this->input->post("name"));
$this->email->to($emailto);
$this->email->subject('New Request');
$this->email->message($body);
if($this->email->send()){
echo json_encode(array("sent"=>TRUE));
}
}
else{
echo json_encode(array("sent"=>FALSE));
}}
Google键是正确的,我不知道为什么它不是
The google keys are correct, I don't know why it is not getting to the true if statement.
推荐答案
如果您使用'g-recaptcha-response'和$ status [ 'success']在给定的实现中返回null,最好使用如下所示的cURL请求方法:
If you use 'g-recaptcha-response' and $status['success'] returns null in your given implementation, it might be better to use the method of cURL request as below:
$google_url = "https://www.google.com/recaptcha/api/siteverify";
$secret_key = 'YOUR_SECRET_KEY';
$response = $_POST['g-recaptcha-response'];
$message = 'Google reCaptcha Test';
if(!empty($response))
{
$url = $google_url."?secret=".$secret_key."&response=".$response;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, TRUE);
$curlData = curl_exec($curl);
curl_close($curl);
$res = json_decode($curlData, TRUE);
if($res['success'] == 'true')
$message = "Success!";
else
$message = "Enter captcha again!";
}
这篇关于验证Recaptcha Ajax和Codeigniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文