ReCaptcha无效+ CodeIgniter表单助手=表单数据丢失

时间:2012-05-08 21:12:47

标签: forms validation codeigniter captcha recaptcha

当我使用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&eacute;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>

为什么会发生这种情况?

1 个答案:

答案 0 :(得分:2)

为了重新填充表单,必须运行form_validation命令。

但是你的问题是,在你的代码中,如果reCaptcha失败,你永远不会运行验证,因此没有什么可以再次填写表单(因为它实际上从未进入验证)。

因此,请更改代码,并首先运行表单验证。如果表单验证返回true,则检查验证码。

if(isset($_POST) && count($_POST)>0){
        $resp = recaptcha_check_answer ($privatekey,
                                        $_SERVER["REMOTE_ADDR"],
                                        $_POST["recaptcha_challenge_field"],
                                        $_POST["recaptcha_response_field"]);
         if($this->form_validation->run()) 
          {
            if ($resp->is_valid) 
            {
                $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;
    }

一件事 - 改进你的代码 - 让你的reCaptcha($ resp-&gt; is_valid)成为一个回调方法 - 然后你可以实际运行验证码作为表单验证的一部分,而不是一个单独的方法。

See here for callbacks

public function _recaptcha_check($str)
    {
              $resp = recaptcha_check_answer ($str,
                                    $_SERVER["REMOTE_ADDR"],
                                    $_POST["recaptcha_challenge_field"],
                                    $_POST["recaptcha_response_field"]);
          if ( ! $resp->is_valid)
          {
            $this->form_validation->set_message('_recaptcha_check', 'Your reCaptcha was wrong!');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }

并将其设置为设置规则的位置(请注意回调名称的双“_”)

array(
                            'field' => 'recaptcha_response_field',
                            'label' => 'Recaptcha',
                            'rules' => 'required|callback__recaptcha_check'),