我有一个简单的联系表单,其中包含几个必填字段,工作正常。我正在尝试将reCaptcha添加到表单中以获得一些额外的垃圾邮件保护。
我正确地在表单上显示了reCaptcha,我已经安装了我的密钥。在我的控制器中,我可以调用recaptcha_check_answer方法并获得有效的响应。我已经正确安装和配置了所有文件,因为我可以使用recaptcha类中的方法并获得良好的响应。
我的问题是我想要一个无效的reCaptcha触发器CI form_validation错误,但我花了很多时间在这上面并且无法触发它。我对CI很陌生,所以如果这很容易,请原谅我的无知。
当is_valid响应返回NULL时,我尝试抛出form_validation错误。如果为NULL,那么我尝试执行以下操作:
$this->form_validation->set_message('recaptcha_response_field', 'reCaptcha', lang('recaptcha_incorrect_response'));
echo form_error('recaptcha_response_field');
以下是有效和无效验证码的reCaptcha API响应:
Valid = stdClass Object ( [0] => is_valid [1] => error [is_valid] => 1 )
Invalid = stdClass Object ( [0] => is_valid [1] => error [is_valid] => [error] => incorrect-captcha-sol )
以下是我在Controller中的代码:
class Contact extends CI_Controller
{
function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->library('recaptcha');
$this->recaptcha->set_key ('MY_PUBLIC_KEY_HERE' ,'public');
$this->recaptcha->set_key ('MY_PRIVATE_KEY_HERE' ,'private'); // For private key
// Is this a Form Submission?
if (!empty($_POST))
{
// LOAD VALIDATION AND SET RULES
$this->load->library('form_validation');
$this->load->helper('form');
$this->form_validation->set_rules('first_name', 'First Name', 'required');
$this->form_validation->set_rules('last_name', 'Last Name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('recaptcha_response_field', 'reCaptcha', 'required|recaptcha_check_answer');
$this->_resp = $this->recaptcha->recaptcha_check_answer ( $this->input->ip_address(), $this->input->post('recaptcha_challenge_field',true), $this->input->post('recaptcha_response_field',true));
// If the reCaptcha doesnt match throw an form_valiation Error
if ($this->_resp->is_valid == '')
{
$this->form_validation->set_message('recaptcha_response_field', 'reCaptcha', lang('recaptcha_incorrect_response'));
echo form_error('recaptcha_response_field');
}
if ($this->form_validation->run() !== FALSE)
{
// SUCCESS NO ERRORS - SEND EMAIL DATA AND RETURN TO HOME
$this->load->library('email'); // LOAD EMAIL LIBRARY
$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] = 'utf-8';
$config['wordwrap'] = TRUE;
$this->email->initialize($config);
$this->email->from($this->input->post('email'), $this->input->post('first_name').' '.$this->input->post('last_name'));
$this->email->to('jassen.michaels@gmail.com');
$this->email->cc('');
$this->email->bcc('');
$this->email->subject('Contact Email From Spider Design Website');
$this->email->message(
'First Name: '.$this->input->post('first_name')."\r\n".
'Last Name: '.$this->input->post('last_name')."\r\n".
'Company: '.$this->input->post('company')."\r\n".
'Location: '.$this->input->post('location')."\r\n"."\r\n".
'I am interested in learning: '."\r\n"."\r\n".
'Information Architecture: '.$this->input->post('information_architecture')."\r\n".
'Web Design: '.$this->input->post('web_design')."\r\n".
'Graphic Design: '.$this->input->post('graphic_design')."\r\n".
'Email Marketing: '.$this->input->post('email_marketing')."\r\n".
'Social Media: '.$this->input->post('social_media')."\r\n".
'Content Management: '.$this->input->post('content_management')."\r\n".
'Search Engine Optimization: '.$this->input->post('seo')."\r\n".
'Web Traffic Analytics: '.$this->input->post('wta')."\r\n"."\r\n".
'Preferred Method of Contact: '."\r\n".
'Phone: '.$this->input->post('phone')."\r\n".
'Email: '.$this->input->post('email')."\r\n"."\r\n".
'Comments: '."\r\n".
$this->input->post('comments')
);
//$this->email->send();
//$this->load->helper('url');
//redirect('http://sdi.myspiderdesign.com/fuel');
}
}
// Not a form submission, load view
$this->load->helper('form');
$this->load->view('include/spider_header');
$this->load->view('contact'); // Content Area
$this->load->view('include/spider_footer'); // Footer
}
}
我已在表单底部注释了重定向和联系电子邮件,直到我可以解决此问题。
提前感谢您的帮助!
答案 0 :(得分:0)
我在form_validation库中使用了callback_函数。在进行API调用的控制器内部设置一个额外的函数,如果验证码没有通过,则会抛出validation_error。