我想在我的注册页面中提供验证码代码的安全性,但是当我用错误的验证码代码填写表单并提交表单时。问题是在我的注册页面中查看两页。并再次使用正确的验证码填写此表格,但是他们又遇到了同样的问题,并显示了错误的验证码。

控制器

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(); ?>

最佳答案

将此代码放在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');
    }


如果您显示消息“帐户创建成功”。使用设定的即显信息
正确阅读此链接

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

How to display error messages in CodeIgniter

关于html - 我的Google验证码代码无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17316614/

10-13 01:01