【发布时间】:2017-01-24 07:04:43
【问题描述】:
我正在尝试将 Google 的 reCAPTCHA 添加到我的 Codeigniter 网络应用中。
但我一直遇到这个问题:
Unable to access an error message corresponding to your field name Captcha. (recaptcha)
我尝试提交表单以抛出错误,$server_output['success'] 应该是 FALSE 从而执行 $this->form_validation->set_message('recaptcha', 'Please redo the {field}.'); 但 Codeigniter 似乎根本没有运行回调函数...
作为错误输出(除了标题中的错误消息),验证适用于其他所有内容:
The Username field is required.
The Password field is required.
The Password Confirmation field is required.
The Email field is required.
我正在通过我的模型运行验证(遵循保持控制器苗条和模型肥大的惯例):
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
}
public function register_validation()
{
/*
* INITIALIZE FORM VALIDATION RULES
*/
$this->form_validation->set_rules('username', 'Username',
array(
// REMOVED FOR PRIVACY REASONS.
)
);
$this->form_validation->set_rules('password', 'Password',
array(
// REMOVED FOR PRIVACY REASONS.
)
);
$this->form_validation->set_rules('password_confirmation', 'Password Confirmation',
array(
// REMOVED FOR PRIVACY REASONS.
)
);
$this->form_validation->set_rules('email', 'Email',
array(
// REMOVED FOR PRIVACY REASONS.
)
);
/*
* SET CUSTOM VERIFICATION RULES:
* 1. GOOGLE RECAPTCHA WIDGET
*/
$this->form_validation->set_rules('g-recaptcha-response', 'Captcha', 'callback_recaptcha');
/*
* CHANGE ERROR MESSAGES FOR SOME RULES.
*/
$this->form_validation->set_message('is_unique', 'The {field} has already been taken!');
/*
* RUN VALIDATION,
* IF VALIDATION IS NOT PASSED --> RETURN FALSE.
*/
if ($this->form_validation->run() === FALSE)
return FALSE;
/*
* ELSE WHEN EVERYTHING PASSES VALIDATION AND RECAPTCHA,
* TRY TO CREATE NEW USER.
*/
else
{
/*
* TRY TO CREATE NEW USER.
*/
if ($this->create_user())
{
/*
* SET FLASHDATA TO ALERT USER THAT
* THE ACCOUNT HAS BEEN SUCCESSFULLY CREATED.
*/
$this->session->account_created = TRUE;
$this->session->mark_as_flash('account_created');
return TRUE;
}
else
{
/*
* THROW ERROR AND ALERT USER VIA FLASHDATA.
*/
$this->session->account_created = FALSE;
$this->session->mark_as_flash('account_created');
return FALSE;
}
}
}
这是我在同一类/模型中的 recaptcha 回调函数:
/*
* RECAPTCHA FUNCTION
*/
public function recaptcha($data = '')
{
/*
* INITIALIZE VARIABLES.
*/
$google_api_url = 'https://www.google.com/recaptcha/api/siteverify';
$google_secret_key = 'REMOVED FOR PRIVACY REASONS';
$ip = $this->input->ip_address();
$response = $data;
/*
* INITIALIZE CURL.
*/
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $google_api_url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS,
http_build_query(array(
'secret' => $google_secret_key,
'response' => $response,
'remoteip' => $ip,
)
)
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
/*
* RUN CURL.
*/
$server_output = curl_exec($curl);
curl_close($curl);
$server_output = json_decode($server_output, true);
/*
* CHECK RECAPTCHA SUCCESS
*/
if ($server_output['success'])
return TRUE;
else
{
$this->form_validation->set_message('recaptcha', 'Please redo the {field}.');
return FALSE;
}
}
我做错了什么?它不起作用是因为我在模型中运行验证吗?已经坚持了 2-3 个小时......我希望代码是可读的。
谢谢~
【问题讨论】:
标签: php codeigniter curl model callback