1

我在 CodeIgniter 应用程序中实现 recaptcha 时遇到问题。问题是 recapctha_challenge_field 和 recaptcha_response_field 没有被发布,但是,recapctcha(和那些字段)在页面上(在表单内)是可见的。recapctha_challenge_field 和 recaptcha_response_field 出现在页面的 HTML 中,但在我发布表单时没有出现在标题中。

我已经下载了 de recaptcha 库并将其添加为 CI 中的助手。在我看来,我在表单中回显了 recaptcha_get_html($publickey) (带有公钥集)。在我的控制器中,我加载了 recaptchalib_helper 并为 recapctha_challenge_field 添加了一个表单验证规则。

这是我的看法:

<h1>Register</h1>

<fieldset>
    <legend>Personal information</legend>

    <?php
    echo form_open('login/create_user');

    echo form_label('First name:', 'first_name');
    echo form_input(
            array(
                'name' => 'first_name',
                'id' => 'first_name',
                'value' => set_value('first_name')
            )    
    );

    echo form_label('Last name:', 'last_name');
    echo form_input(
            array(
                'name' => 'last_name',
                'id' => 'last_name',
                'value' => set_value('last_name')
            )
    );

    echo form_label('Birth date:', 'birth_date');
    echo form_input(
            array(
                'name' => 'birth_date',
                'id' => 'birth_date',
                'value' => set_value('birth_date')
            )
    );

    echo form_label('E-mail:', 'email');
    echo form_input(
            array(
                'name' => 'email',
                'id' => 'email',
                'value' => set_value('email')
            )
    );
    ?>
</fieldset>

<fieldset>
    <legend>Login information</legend>

    <?php
    echo form_label('Username:', 'username');
    echo form_input(
            array(
                'name' => 'username',
                'id' => 'username',
                'value' => set_value('username')
            )
    );

echo form_label('Password:', 'password1');
echo form_password(
        array(
            'name' => 'password1',
            'id' => 'password1',
            'value' => set_value('password1')
        )
);

echo form_label('Confirm password:', 'password2');
echo form_password(
        array(
            'name' => 'password2',
            'id' => 'password2',
            'value' => set_value('password2')
        )
);

$publickey = "mypublickey"; // here I entered my public key
echo recaptcha_get_html($publickey);

echo form_label(nbs(1), 'submit');
echo form_submit(
        array(
            'name' => 'submit',
            'id' => 'submit',
            'value' => 'Registreren'
        )
    );

    echo form_close();
    ?>

    <?php echo validation_errors('<p class="error">'); ?>
</fieldset>
</div>

这是我的控制器的一部分:

function create_user() {
    print_r($_POST);//for debugging
    $this->load->library('form_validation'); 
    $this->load->model('user');

    $this->form_validation->set_rules('recaptcha_challenge_field', 'Captcha', 'callback_validate_captcha');
    $this->form_validation->set_rules('first_name', 'First name', 'trim|xss_clean|required');
    $this->form_validation->set_rules('last_name', 'Last name', 'trim|xss_clean|required');
    $this->form_validation->set_rules('email', 'E-mail', 'trim|xss_clean|valid_email|callback_is_email_available|required');

    $this->form_validation->set_rules('username', 'Username', 'trim|xss_clean|min_length[5]|callback_is_username_available|required');
    $this->form_validation->set_rules('password1', 'Password', 'trim|xss_clean|min_length[8]|max_length[32]|required');
    $this->form_validation->set_rules('password2', 'Confirm password', 'trim|xss_clean|matches[password1]|required');

    if ($this->form_validation->run() == FALSE) {
        $this->signup();
    } else {
        if ($this->user->create_user($this->input->post('username'),$this->input->post('password1'),$this->input->post('email'),$this->input->post('first_name'),$this->input->post('last_name'),$this->input->post('birth_date'))) {
            $data['main_content'] = 'login/signup_successful';
            $this->load->view('includes/template', $data);
        } else {
            $this->load->view('login/signup_form');
        }
    }
}

 public function validate_captcha($recaptcha_challenge_field) {
    $this->load->helper('recaptchalib');
    $privatekey = "myprivatekey";//this is et to my private key
    $resp = recaptcha_check_answer ($privatekey,
                                    $this->input->ip_address(),
                                    $this->input->post("recaptcha_challenge_field"),
                                    $this->input->post("recaptcha_response_field"));

    if (!$resp->is_valid) {
        $this->form_validation->set_message('validate_captcha', 'Invalid Capctha code entered.');
        return FALSE;
    } else {
        return TRUE;
    }
}

验证码字段未在 HTTP 标头中设置:

Form data:
csrf_test_name:8cc3f2391784867df2d46f193a65a317
first_name:Myfirstname
last_name:Mylastname
birth_date:10-12-2012
email:myemail@adres.com
username:username
password1:password
password2:password
submit:Register

我究竟做错了什么?

忠实于你的,

阿尔文

4

1 回答 1

0

表单标签不是在表格、tbody 或 tr 中开始吗?

于 2012-12-13T06:11:39.487 回答