1

我希望我的注册页面中的验证码安全,但是当我用错误的验证码填写表格并提交表格时。问题是在我的注册页面中查看两个页面。并用正确的验证码再次填写此表格,但他们再次出现同样的问题并显示不正确的验证码。

控制器

public function user_signup($data = NULL){
                $this->load->library('form_validation');
                $this->recaptcha->recaptcha_check_answer($_SERVER['REMOTE_ADDR'],$this->input->post('recaptcha_challenge_field'),$this->input->post('recaptcha_response_field'));

                $this->form_validation->set_rules('Username', 'Username', 'required|trim|is_unique[users.Username]');
                $this->form_validation->set_rules('First_name', 'First Name', 'required|trim');
                $this->form_validation->set_rules('Last_name', 'Last Name', 'required|trim');
                $this->form_validation->set_rules('Email', 'Email', 'required|trim|valid_email');
                $this->form_validation->set_rules('Password', 'Password', 'required|trim|md5');
                $this->form_validation->set_rules('Conf_password', 'Confirm Password', 'required|trim|matches[Password]');
                $this->form_validation->set_message('is_unique', 'Username already exists.');
                if($this->form_validation->run() && $this->recaptcha->getIsValid()){
                    //Call to recaptcha to get the data validation set within the class. 

                    $form_data = array();
                        $form_data['Username'] = $this->input->post('Username');
                        $form_data['First_name'] = $this->input->post('First_name');
                        $form_data['Last_name'] = $this->input->post('Last_name');
                        $form_data['Email'] = $this->input->post('Email');
                        $form_data['Password'] = $this->input->post('Password');
                        $form_data['Conf_password'] = $this->input->post('Conf_password');

                        $this->load->model('User');
                        $this->User->sign_up($form_data);
                        $data['msg']= "Account Created Sucessfuly";

                       }else{
                        if(!$this->recaptcha->getIsValid()){
                        $this->session->set_flashdata('error','incorrect captcha');                     
                        }
                    }

                redirect("Users/signup", $data);
        }

模型

public function sign_up($form_data){
       $this->db->insert('users', $form_data);
}

看法

                            <?php
                            //$val = NULL;
                            if(isset($results->id)){
                                $val = "Update";
                                $action = "Users/user_update/".$results->id;
                            }else{
                                $val= "Register";
                                $action = "Users/user_signup";
                            }; ?>

                            <?php echo form_open($action); ?>
                            <?php echo form_input(array('name'=>'Username', 'class'=>'input-xlarge', 'type'=>'text', 'placeholder'=>'Username', 'required' => 'required', 'value'=>$u)); ?>
                            <?php echo form_error('Username', '<div class="alert alert-error">', '</div>'); ?>
                            <?php echo form_input(array('name'=>'First_name', 'class'=>'input-xlarge', 'type'=>'text', 'placeholder'=>'First Name', 'required' => 'required', 'value'=>$f)); ?>
                            <?php echo form_error('First_name', '<div class="alert alert-error">', '</div>'); ?>
                            <?php echo form_input(array('name'=>'Last_name', 'class'=>'input-xlarge', 'type'=>'text', 'placeholder'=>'Last Name', 'required' => 'required', 'value'=>$l)); ?>
                            <?php echo form_error('Last_name', '<div class="alert alert-error">', '</div>'); ?>
                            <?php echo form_input(array('name'=>'Email', 'class'=>'input-xlarge', 'type'=>'text', 'placeholder'=>'Email', 'required' => 'required', 'value'=>$e)); ?>
                            <?php echo form_error('Email', '<div class="alert alert-error">', '</div>'); ?>
                            <?php echo form_input(array('name'=>'Password', 'class'=>'input-xlarge', 'type'=>'password', 'placeholder'=>'Password')); ?>
                            <?php echo form_error('Password', '<div class="alert alert-error">', '</div>'); ?>
                            <?php echo form_input(array('name'=>'Conf_password', 'class'=>'input-xlarge', 'type'=>'password', 'placeholder'=>'Confirm Password')); ?>                           
                            <?php echo form_error('Conf_password', '<div class="alert alert-error">', '</div>'); ?>

                            <?php if(!isset($results->id)){echo $recaptcha_html;} ?>
                            <?php if ($this->session->flashdata('error') !== FALSE) { echo '<div class="alert alert-error">'.$this->session->flashdata('error').'</div>'; } ?>
                         <div class="login-actions">                           
                             <span><input type="checkbox"> <span class="remember">I have read & agree</span></span>                 
                             <span><?php echo form_submit(array('value'=>$val, 'class'=>'btn btn-large btn-warning pull-right', 'type'=>'submit')); ?></span>
                        </div>
                    <?php echo form_close(); ?>
4

2 回答 2

0

首先,您在Controller中创建一个注册功能并查看默认注册页面。就像

        $this->load->view('User/User_sign_up');
于 2013-06-27T12:33:48.187 回答
0

将此代码放在if(!$this->recaptcha->getIsValid()){ 之后并回显消息...

    if($this->input->post('recaptcha_response_field') =="" ){
         $this->session->set_flashdata('error','fill up this code');
    }else{
         $this->session->set_flashdata('error','incorrect captcha');
    }

如果您显示消息“帐户创建成功”。使用set flash message 正确阅读此链接

http://ellislab.com/codeigniter/user-guide/libraries/sessions.html

如何在 CodeIgniter 中显示错误消息

于 2013-06-27T13:03:56.787 回答