当我使用 codeIgniter 和 ReCaptcha 时,这是一个奇怪的行为。
我使用 ReCaptcha 作为一个简单的应用程序/助手。我所做的唯一更改是将 API 重命名为“recaptcha_helper.php”,然后我添加了
if(!defined('RECAPTCHA")){
define('RECAPTCHA',true);
[API code]
}
将数据发布到我的控制器时,结果是......不像预期的那样。
ReCaptcha valid / form valid = works fine!
ReCaptcha valid / form not valid = works fine!
ReCaptcha not valid / form valid = all form data lost
ReCaptcha not valid / form not valid = all form data lost + validation lost
我也像我制作的所有其他网站一样使用“set_value('input_name')”。直到今天,当我将 recaptcha 放入表单中时,它曾经像魅力一样发挥作用。
这是控制器:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Register extends MY_Controller {
var $form_rules = array(
array(
'field' => 'user_first_name',
'label' => 'Prénom ',
'rules' => 'trim|required|max_lenght[50]|xss_clean'),
array(
'field' => 'user_last_name',
'label' => 'Nom ',
'rules' => 'trim|required|max_lenght[50]|xss_clean'),
array(
'field' => 'user_email',
'label' => 'Courriel ',
'rules' => 'trim|required|max_lenght[100]|valid_email|xss_clean'),
array(
'field' => 'user_email_confirm',
'label' => 'Confirmation du courriel ',
'rules' => 'trim|required|max_lenght[100]|matches[user_email]|xss_clean'));
public function __construct(){
parent::__construct();
}
public function register(){
$this->load->library('form_validation');
$this->form_validation->set_rules($this->form_rules);
// RECAPTCHA STUFF
$this->load->helper('recaptcha');
$publickey = "****";
$privatekey = "****";
# the response from reCAPTCHA
$resp = null;
# the error code from reCAPTCHA, if any
$error = null;
if(isset($_POST) && count($_POST)>0){
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if ($resp->is_valid) {
if($this->form_validation->run()){
$new_data = array( 'user_first_name' => $_POST['user_first_name'],
'user_last_name' => $_POST['user_last_name'],
'user_email' => $_POST['user_email']);
$this->db->insert('user', $new_data);
$new_user_id = $this->db->insert_id();
$this->load->view('header');
$this->load->view('sent');
$this->load->view('footer');
return;
}
}else{
$error = $resp->error;
}
$data = $_POST;
}
$data['recaptcha'] = recaptcha_get_html($publickey, $error);
$this->load->view('header');
$this->load->view('inscription_form', $data);
$this->load->view('footer');
}
}
这是表格
<div id="inscription">
<?php echo form_open(); ?>
<p>
<label for="user_first_name">Prénom *</label>
<input type="text" name="user_first_name" value="<?php echo set_value('user_first_name'); ?>" maxlength="50" />
<?php echo form_error('user_first_name'); ?>
</p>
<p class="clear">
<label for="user_last_name">Nom *</label>
<input type="text" name="user_last_name" value="<?php echo set_value('user_last_name'); ?>" maxlength="50" />
<?php echo form_error('user_last_name'); ?>
</p>
<p class="clear">
<label for="user_email">Courriel *</label>
<input type="text" name="user_email" value="<?php echo set_value('user_email'); ?>" maxlength="100" />
<?php echo form_error('user_email'); ?>
</p>
<p class="clear">
<label for="user_email_confirm">Confirmation du courriel *</label>
<input type="text" name="user_email_confirm" value="<?php echo set_value('user_email_confirm'); ?>" maxlength="100" />
<?php echo form_error('user_email_confirm'); ?>
</p>
<div class="clear"></div>
<div><?php echo $recaptcha; ?></div>
<p><input type="submit" value="Envoyer"/></p>
<p class="clear">* Tous les champs de ce formulaire sont requis.</p>
</form>
</div>
任何想法为什么会发生这种情况?